首先我们上图:
xml的代码如下,用于编写按钮:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:background="@drawable/background"xmlns:Widget="http://schemas.androID.com/apk/res-auto"androID:orIEntation="vertical"> <button androID:ID="@+ID/button_1" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="简单的dialog" /> <button androID:ID="@+ID/button_2" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="列表的dialog" /> <button androID:ID="@+ID/button_3" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="单选的dialog" /> <button androID:ID="@+ID/button_4" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="多选的dialog" /></linearLayout>
Java代码如下,用于实现逻辑:
import androIDx.appcompat.app.Actionbar;import androIDx.appcompat.app.AlertDialog;import androIDx.appcompat.app.AppCompatActivity;import androID.content.DialogInterface;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.Toast;public class MainActivity extends AppCompatActivity{ int index; String [] item = {"AndroID","IOS","Spark","Hadoop","Web"}; boolean[] bools = {false,false,false}; // 设置boolean数组所有的选项设置默认没选 @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); Actionbar actionbar = getSupportActionbar(); if (actionbar != null) { actionbar.hIDe(); } button button=(button)findVIEwByID(R.ID.button_1); button.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.girl); builder.setTitle("标题栏"); builder.setMessage("对话框内容,可自行设置"); builder.setPositivebutton("确定",new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog,int which) { Toast.makeText(MainActivity.this,"点击了确定",Toast.LENGTH_SHORT).show(); } }); builder.setNegativebutton("取消",new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialogInterface,int i) { Toast.makeText(MainActivity.this,"点击了取消",Toast.LENGTH_SHORT).show(); } }); builder.setNeutralbutton("好的","点击了“好的”",Toast.LENGTH_SHORT).show(); } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }); button button2=(button)findVIEwByID(R.ID.button_2); button2.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("请选择一个技术分支"); builder.setItems(item,"选择了"+item[which],Toast.LENGTH_SHORT).show(); } }); // 取消可以不添加 //builder.setNegativebutton("取消",null); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }); button button3=(button)findVIEwByID(R.ID.button_3); button3.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("请选择技术分支:"); builder.setSingleChoiceItems(item,index,int which) { index = which; } }); builder.setPositivebutton("确定","选择了"+item[index],null); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }); button button4=(button)findVIEwByID(R.ID.button_4); button4.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("请选择技术分支:"); builder.setMultiChoiceItems(item,bools,new DialogInterface.OnMultiChoiceClickListener() { @OverrIDe public voID onClick(DialogInterface dialog,int which,boolean isChecked) { bools[which] = isChecked; } }); builder.setPositivebutton("确定",int which) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < item.length; i++) { if (bools[i]) { sb.append(item[i] + " "); } } Toast.makeText(MainActivity.this,"选择了" + sb.toString(),null); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }); }}
总结
以上所述是小编给大家介绍的AndroID中AlertDialog四种对话框的最科学编写用法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
以上是内存溢出为你收集整理的Android中AlertDialog四种对话框的最科学编写用法(实例代码)全部内容,希望文章能够帮你解决Android中AlertDialog四种对话框的最科学编写用法(实例代码)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)