怎么在Android中使用AlertDialog实现对话框?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
成都创新互联专注于企业营销型网站、网站重做改版、向阳网站定制设计、自适应品牌网站建设、html5、商城网站定制开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为向阳等各大城市提供网站开发制作服务。
xml的代码如下,用于编写按钮:
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,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("好的", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this, "点击了“好的”", 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, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "选择了"+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, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { index = which; } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "选择了"+item[index], Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",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("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, 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(), Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",null); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }); } }
Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。
看完上述内容,你们掌握怎么在Android中使用AlertDialog实现对话框的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!