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

目标

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

EditText点击事件——弹出单选框_第1张图片


1,设置EditText属性

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

android:cursorVisible="false"
android:editable="false"
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”

EditText点击事件——弹出单选框_第2张图片


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">15spitem>
  style>

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

<resources>

    
    <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>

    
    <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">15spitem>
  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;

你可能感兴趣的:(Android/Java)