EditText点击事件——弹出单选框

来源:互联网 发布:樱井知香黑人 编辑:程序博客网 时间:2024/06/01 09:10

目标

点击Faculty弹出院系单选对话框,将选择的内容显示在EditText中
Faculty是一个Edittext控件。
这里写图片描述

这里写图片描述


1,设置EditText属性

使用以下两句设置EditText为不可输入且不可弹出输入法

android:cursorVisible="false"android:editable="false"
<EditText            android:text=""             android:id="@+id/etFaculty"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_below="@+id/etBirthday"            android:cursorVisible="false"            android:editable="false"            android:hint="Faculty"         />

2,设置EditText的点击事件,使用setOnTouchListener

private final static int FACULTY_DIA = 2;private EditText etFaculty;@Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_personal_info);        etFaculty = (EditText) findViewById(R.id.etFaculty);        etFaculty.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                // TODO Auto-generated method stub                showDialog(FACULTY_DIA);                return false;            }        });

3,创建一个院系名的array资源 “faculty”

这里写图片描述


4,创建单选对话框

protected Dialog onCreateDialog(int id) {        Dialog dialog = null;        switch (id) {        case FACULTY_DIA:             Builder builder1 = new android.app.AlertDialog.Builder(this);            // 设置对话框的图标            builder.setIcon(R.drawable.header);            // 设置对话框的标题        Builder setTitle1 = builder1.setTitle("Faculty");            builder1.setSingleChoiceItems(R.array.faculty, 0, new OnClickListener() {                public void onClick(DialogInterface dialog, int which) {                    String faculty = getResources().getStringArray(R.array.faculty)[which];                    etFaculty.setTextSize(15);                    etFaculty.setText(faculty);                }            });            // 添加一个确定按钮            builder1.setPositiveButton(" OK ", new DialogInterface.OnClickListener() {                public void onClick(DialogInterface dialog, int which) {                }            });            // 创建一个单选按钮对话框            dialog = builder1.create();            break;}

5,修改对话框的字体大小

在styles.xml定义对话框字体大小:

    </style>    <style name="dialog" parent="@android:style/Theme.Dialog">    <item name="android:textSize">15sp</item>  </style>

加入后styles.xml的内容如下所示:

<resources>    <!--        Base application theme, dependent on API level. This theme is replaced        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.    -->    <style name="AppBaseTheme" parent="android:Theme.Light">        <!--            Theme customizations available in newer API levels can go in            res/values-vXX/styles.xml, while customizations related to            backward-compatibility can go here.        -->    </style>    <!-- Application theme. -->    <style name="AppTheme" parent="AppBaseTheme">        <!-- All customizations that are NOT specific to a particular API-level can go here. -->    </style>    <style name="dialog" parent="@android:style/Theme.Dialog">    <item name="android:textSize">15sp</item>  </style></resources>

修改创建单选对话框的代码:

case FACULTY_DIA:            // Dialog dialog = null;            ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(this, R.style.dialog);            Builder builder1 = new AlertDialog.Builder(contextThemeWrapper);            // Builder builder1 = new android.app.AlertDialog.Builder(this);            // 设置对话框的图标            // builder.setIcon(R.drawable.header);            // 设置对话框的标题            Builder setTitle1 = builder1.setTitle("Faculty");            // 0: 默认第一个单选按钮被选中            builder1.setSingleChoiceItems(R.array.faculty, 0, new OnClickListener() {                public void onClick(DialogInterface dialog, int which) {                    String faculty = getResources().getStringArray(R.array.faculty)[which];                    etFaculty.setTextSize(15);                    etFaculty.setText(faculty);                }            });            // 添加一个确定按钮            builder1.setPositiveButton(" OK ", new DialogInterface.OnClickListener() {                public void onClick(DialogInterface dialog, int which) {                }            });            // 创建一个单选按钮对话框            dialog = builder1.create();            break;
0 0