目标

点击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;

更多相关文章

  1. 第三十六天 一乐在其中—Android的按钮单击事件及监听器的实现方
  2. Android中AlertDialog实现三种对话框
  3. Android--(11)--解读单选(RadioButton)和复选(CheckBox)按钮
  4. 定制Android设备的关机对话框
  5. android 笔记 --- 按钮上面的文字滚动起来
  6. Android中的diglog对话框
  7. Android 仿淘宝选中商品不同尺寸的按钮组(二)
  8. 【Android】利用Java代码布局,按钮添加点击事件
  9. Android 自定义Button按钮显示样式(正常、按下、获取焦点)

随机推荐

  1. Load bitmap from file Android
  2. android获取google邮箱
  3. Android弹出和收起输入法键盘
  4. android各种图片获取路径的方式
  5. Android(安卓)签名证书
  6. Android获取、设置Wifi状态
  7. Android中setContentView(int laoutId)
  8. android input系统如何导入kl文件
  9. android 添加sensor 权限
  10. [Android实例] android多点触摸demo .