Android创建AlertDialog对话框 ==之一
16lz
2021-01-26
activity如下
package com.example;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class LifeActivity extends Activity {private Button alert = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);alert = (Button)findViewById(R.id.alert);final Builder builder = new AlertDialog.Builder(this);OnClickListener cliclListener = new OnClickListener() {@Overridepublic void onClick(View v) {//测试弹出窗口builder.setIcon(R.drawable.ic_launcher);builder.setMessage("测试弹出窗口");builder.setTitle("自定义普通对话框");builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(LifeActivity.this, "点击了确定按钮", Toast.LENGTH_SHORT).show();}});builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(LifeActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();}});builder.create();builder.show();}};alert.setOnClickListener(cliclListener);}}main.xml中就一个按钮
<Button android:id="@+id/alert" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="AlertDialog"/>
//设置对话框的图标builder.setIcon(R.drawable.ic_launcher);//设置对话框的标题builder.setTitle("自定义普通对话框");builder.setItems(new String[]{"red","yellow","blue","green"}, new DialogInterface.OnClickListener(){//该方法which参数代表用户单击的那个列表项@Overridepublic void onClick(DialogInterface dialog, int which) {switch(which){case 0:Toast.makeText(LifeActivity.this, "red", Toast.LENGTH_SHORT).show();break;case 1:Toast.makeText(LifeActivity.this, "yellow", Toast.LENGTH_SHORT).show();break;case 2:Toast.makeText(LifeActivity.this, "blue", Toast.LENGTH_SHORT).show();break;case 3:Toast.makeText(LifeActivity.this, "green", Toast.LENGTH_SHORT).show();break;}}});builder.create().show();
更多相关文章
- android Dialog给Activity 传参
- radioButton 使用
- Android(安卓)studio取消上次打开的工程
- 自定义对话框 位置 大小等
- Android短信发送程序核心代码
- android customDialog自定义对话框
- Android带删除按钮的ListView
- Android(安卓)studio button 按钮 四种绑定事件的方法
- android srcollview按钮顶部停留