在这篇文章首先介绍怎么在应用中弹出一个对话框AlertDialog以及相关的设置,然后在制定一个自定义的AlertDialog

AlertDialog的创建,在activity的布局文件中,加入一个按钮btn_click,并在活动代码里为其设置点击监听,在监听事件里弹出AlertDialog,下面是按钮的点击事件代码

 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);        builder.setTitle("对话框标题")                .setMessage("提示")                .setNegativeButton("取消", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        //取消按钮点击事件                        Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();                    }                })                .setPositiveButton("确定", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        //确定按钮点击事件                        Toast.makeText(MainActivity.this, "你点击了确定", Toast.LENGTH_SHORT).show();                    }                });        AlertDialog dialog = builder.create();        dialog.show();
 **自定义AlertDialog** 先要新建一个布局文件custom_dialog_layout,里面放置按钮和文本 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView  android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="个人信息" android:gravity="center" android:id="@+id/textView" android:textStyle="bold" android:background="#888"/> <LinearLayout  android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView  android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="姓名:" android:textSize="24dp" /> <EditText  android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:id="@+id/et_name"/> </LinearLayout> <LinearLayout  android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView  android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="电话:" android:textSize="24dp" /> <EditText  android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:id="@+id/et_tel"/> </LinearLayout> <LinearLayout  android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button  android:layout_width="wrap_content" android:layout_weight="1" android:text="提交" android:id="@+id/btn_submit" android:layout_height="fill_parent"/> <Button  android:layout_width="wrap_content" android:layout_weight="1" android:text="取消" android:id="@+id/btn_cancel" android:layout_height="fill_parent"/> </LinearLayout> </LinearLayout>

在activity.java中可以在点击事件里面这样写了
先通过Inflater.inflate()获得
View layout=getLayoutInflater.from(this).inflate(R.layout.custom_dialog_layout,null,false);

然后我们获得layout里面控件以及为按钮添加点击事件:

接下来为dialog 指定ContentView,整个点击事件代码

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);        final AlertDialog dialog = builder.create();       //获得view        View layout = getLayoutInflater().from(this).inflate(R.layout.custom_dialog_layout, null, false);        //获得view中的控件        final EditText et_name = (EditText) layout.findViewById(R.id.et_name),                et_tel = (EditText) layout.findViewById(R.id.et_tel);        Button btn_submit = (Button) layout.findViewById(R.id.btn_submit),                btn_cancel = (Button) layout.findViewById(R.id.btn_cancel);        //为按钮添加事件监听        btn_submit.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                dialog.dismiss();                String str="name="+et_name.getText().toString()+",tel="+et_tel.getText().toString();                Toast.makeText(MainActivity.this,str, Toast.LENGTH_SHORT).show();            }        });        btn_cancel.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                dialog.dismiss();            }        });        dialog.show();        dialog.setContentView(layout);

运行结果如图。

问题出现了,输入文本框聚焦后并不弹出输入框。。。。

原因很。。。参考一下这篇博客 关于AlertDialog中EditText不能弹出输入法解决方法

只需要将setContentView (layout)改为setView(layout)

好了看看效果:

更多相关文章

  1. ViewPager + Fragment 替换 TabActivity
  2. android之App Widget开发实例
  3. 自定义datePicker的实现
  4. Android在子线程更新UI主线程的6种方法
  5. Android(安卓)Studio实现两次返回键退出
  6. ViewPage滑动加载大图和点击关注效果《IT蓝豹》
  7. Android(安卓)ListView中onItemClickListener事件失效的解决方法
  8. Android(安卓)单选按钮RadioButton的使用
  9. Android环境搭建_

随机推荐

  1. android permission权限与安全机制解析(下
  2. android修炼大法
  3. android repo/git server 建立过程(1)
  4. 实现自己的Camera
  5. 在Android中引入Java8的lambda表达式
  6. Android4.0:统一标准强行安装默认主题Holo
  7. 硬件访问服务4之Android硬件访问服务框架
  8. Android深度探索(卷1):安装C/C++交叉编译环
  9. Android构建过程简述
  10. ThreadLocal原理