Android基础——对话框
Posted zsbenn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android基础——对话框相关的知识,希望对你有一定的参考价值。
写了四种对话框
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定取消对话框" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="列表对话框"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="单选列表框"/> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="多选列表框"/> </LinearLayout>
java代码
package com.example.mydialogi; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.app.Dialog; 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 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* * 带“确定”“取消”按钮的对话框 * */ Button button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //创建一个AlterDialog对象 AlertDialog alertDialog = new AlertDialog.Builder( MainActivity.this ).create(); alertDialog.setIcon(R.drawable.b); alertDialog.setTitle("标题"); alertDialog.setMessage("内容"); //设置按钮及其响应 alertDialog.setButton( DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText( MainActivity.this,"选择了取消",Toast.LENGTH_SHORT ).show(); } } ); alertDialog.setButton( DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText( MainActivity.this,"选择了确定",Toast.LENGTH_SHORT ).show(); } } ); alertDialog.show(); } }); /* * 列表对话框 * */ Button button2 = (Button)findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //带四个列表项的列表对话框 final String[] Items = new String[]{ "A","B","C","D" }; //创建一个Builder对象 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.b); builder.setTitle("标题"); //添加列表项 builder.setItems(Items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText( MainActivity.this,"选择了【"+Items[which]+"】",Toast.LENGTH_SHORT ).show(); } }); builder.create().show();//创建并显示对话框 } }); Button button3 = (Button)findViewById(R.id.button3); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //带单选列表项的对话框 final String[] items = new String[]{ "A","B","C","D" }; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("标题"); builder.setIcon(R.drawable.b); //添加列表项 builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText( MainActivity.this,"选择的是【"+items[which]+"】",Toast.LENGTH_SHORT ).show(); } }); //添加确定按钮 builder.setPositiveButton("确定",null); builder.create().show(); } }); /* * 带多选列表项的对话框 * */ Button button4 = (Button)findViewById(R.id.button4); button4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final boolean[] checkedItems = new boolean[]{ false,true,false,true,false }; final String[] items = new String[]{ "A","B","C","D","E" }; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.b); builder.setTitle("标题"); builder.setMultiChoiceItems( items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { checkedItems[which]=isChecked;//改变被操作选项的状态 } } ); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String result = ""; for(int i=0;i<items.length;i++){ if(checkedItems[i]==true){ result += items[i] + " "; } } if(!"".equals(result)){ Toast.makeText( MainActivity.this,"已选择了【"+result+"】",Toast.LENGTH_SHORT ).show(); } } });//添加确定按钮 builder.create().show(); } }); } }
以上是关于Android基础——对话框的主要内容,如果未能解决你的问题,请参考以下文章