定义单选列表dialog

作者: utf8_1436 | 来源:发表于2017-06-18 15:37 被阅读65次

    dialog有很多种,这个例子演示的是单选列表dialog;
    1数据由外部来定义和传入;
    2具体的item响应事件也由具体的调用者来实现,dialog内部定义接口即可。


    demo的演示


    GIF.gif

    dialog的代码如下:

    package study.lzl.animationtest.view.dialog;
    
    import android.app.Dialog;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.Toast;
    
    import java.util.List;
    
    import study.lzl.animationtest.R;
    
    /**
     * @author: Created by lzl on 2017/6/17.
     * @function: 自定义单选列表dialog
     * @description:
     */
    
    public class SingleSelectDialog extends Dialog {
        private ImageView mCloseImg;//关闭按钮
        private Button mSureBtn;//确定选择按钮
        private ListView mListView;//列表
        private Context mContext;
        private List<String> mList;
        /**
         * 记录当前选中的值
         */
        private String currentSelectItemName;
        /**
         * 是否被点击了 如果被点击了 则此时可以点击确定按钮
         */
        private boolean isBeClick=false;
    
        public SingleSelectDialog(Context context,List<String> list) {
            super(context, R.style.MyDialog);
            this.mContext=context;
            this.mList=list;
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_singleselect_dialog);
            //点击外部区域不可取消
            setCanceledOnTouchOutside(false);
            initView();
            initListViewData();
            initEvent();
        }
    
        /**
         * 加载列表数据
         */
        private void initListViewData() {
            mListView.setAdapter(new ArrayAdapter<String>(mContext,R.layout.layout_singleselect_dialog_item,mList));
        }
    
        private void initView() {
            mCloseImg= (ImageView) findViewById(R.id.single_select_close_btn);
            mSureBtn= (Button) findViewById(R.id.single_select_sure_btn);
            mListView= (ListView) findViewById(R.id.singselect_list);
    
        }
    
        private void initEvent() {
            mCloseImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss();
                }
            });
            mSureBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                if (currentSelectItemName!=null&&!currentSelectItemName.equals("")){
                    Toast.makeText(mContext,"你当前选中的item"+currentSelectItemName,Toast.LENGTH_SHORT).show();
                    }else {
                    Toast.makeText(mContext,"您当前未选择任何项",Toast.LENGTH_SHORT).show();
                }
    
                }
            });
    
            mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                    if(mList!=null){
                        currentSelectItemName=mList.get(position);
                    }
                }
            });
        }
    }
    

    布局文件:layout_singleselect_dialog.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="200dp"
        android:layout_height="300dp"
        android:background="@drawable/free_dialog_bg">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal"
            android:layout_margin="5dp">
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="8"
                android:text="单选列表dialog"
                android:textSize="15dp"
                android:textColor="@color/blue"
                android:gravity="center"/>
            <ImageView
                android:id="@+id/single_select_close_btn"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@mipmap/close_small"
                android:layout_gravity="center_vertical"/>
        </LinearLayout>
        <ListView
            android:id="@+id/singselect_list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
    
        </ListView>
        <Button
            android:id="@+id/single_select_sure_btn"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:background="@drawable/close_btn_bg"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="30dp"
            android:text="确定"/>
    </LinearLayout>
    

    布局文件用到的drawable资源文件:free_dialog_bg.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <!--背景色-->
        <solid android:color="#ffffff" />
        <!--描边-->
        <stroke
            android:width="0.8dp"
            android:color="#ffffff" />
        <!-- 圆角 -->
        <corners android:radius="10dp" />
    
    </shape>
    

    确定按钮用到的背景drawable资源文件:close_btn_bg.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <!--填充的背景色-->
        <solid android:color="@color/colorAccent"/>
        <corners android:radius="20dp" />
    </shape>
    

    相关文章

      网友评论

        本文标题:定义单选列表dialog

        本文链接:https://www.haomeiwen.com/subject/hngiqxtx.html