AlertDialog创建6种对话框的用法 (对话框,单选对话框,多选对话框,列表对话框,自定义对话框,退出对话框)
来源:互联网 发布:网络工作 编辑:程序博客网 时间:2024/05/16 17:33
实例如图:
acticity.xml布局文件
<?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" tools:context="com.yongninggo.helloworld.MainActivity" android:orientation="vertical" android:gravity="center_horizontal"> <Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="对话框" android:layout_marginTop="10dip"/> <Button android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="单选对话框" android:layout_marginTop="10dip"/> <Button android:id="@+id/btn3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="多选对话框" android:layout_marginTop="10dip"/> <Button android:id="@+id/btn4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="列表对话框" android:layout_marginTop="10dip"/> <Button android:id="@+id/btn5" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定义对话框" android:layout_marginTop="10dip"/></LinearLayout>
activity.java文件
package com.yongninggo.helloworld;import android.content.DialogInterface;import android.os.Bundle;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.view.LayoutInflater;import android.view.View;import android.widget.Button;import android.widget.Toast;public class Activity extends AppCompatActivity { private Button btn1; private Button btn2; private Button btn3; private Button btn4; private Button btn5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); btn3 = (Button) findViewById(R.id.btn3); btn4 = (Button) findViewById(R.id.btn4); btn5 = (Button) findViewById(R.id.btn5); final String[] ma = {"男","女"}; btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this); builder.setIcon(R.drawable.image1); builder.setTitle("提示"); builder.setMessage("提示框内容"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(Activity.this,"您点击了确定",Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",null); builder.show(); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this); builder.setIcon(R.drawable.image1); builder.setTitle("提示"); builder.setSingleChoiceItems(ma, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(Activity.this,"您选择了:"+ma[which],Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(Activity.this,"您点击了"+ma[which],Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",null); builder.show(); } }); btn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this); builder.setIcon(R.drawable.image1); builder.setTitle("提示"); builder.setMultiChoiceItems(ma, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(Activity.this,"您点击了:"+ma[which],Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(Activity.this,"您点击了"+ma[which],Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",null); builder.show(); } }); btn4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this); builder.setIcon(R.drawable.image1); builder.setTitle("提示"); builder.setItems(ma, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(Activity.this,"您点击了:"+ma[which],Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(Activity.this,"您点击了"+ma[which],Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",null); builder.show(); } }); btn5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { LayoutInflater inflater = LayoutInflater.from(Activity.this); View view = inflater.inflate(R.layout.activity1,null); AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this); builder.setView(view); builder.show(); } }); }}
activity1.xml布局文件
<?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" tools:context="com.yongninggo.helloworld.MainActivity" android:orientation="vertical" android:gravity="center_horizontal"> <LinearLayout android:id="@+id/lin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dip" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自定义Toast" android:gravity="center_horizontal"/> <ImageView android:layout_width="300dip" android:layout_height="182dp" android:src="@drawable/image2"/> </LinearLayout></LinearLayout>
onBackPressed退出操作提示框 (退出程序写在主入口的结尾)
@Override public void onBackPressed() { //super.onBackPressed(); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.image1); builder.setTitle("提示"); builder.setMessage("提示框内容"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }); builder.setNegativeButton("取消",null); builder.show(); }
阅读全文
0 0
- AlertDialog创建6种对话框的用法 (对话框,单选对话框,多选对话框,列表对话框,自定义对话框,退出对话框)
- Android简单对话框、输入对话框、单选对话框、复选对话框、列表对话框、自定义对话框、时间对话框
- Android-Dialog对话框 全解(普通对话框,单选对话框,多选对话框,列表对话框,自定义对话框,Activity对话框)
- 各种对话框的实现(文字对话框,选择对话框,弹出输入对话框,单选,多选对话框,自定义对话框)
- 使用AlertDialog创建单选列表对话框
- 使用AlertDialog创建单选列表对话框
- AlertDialog - 自定义单选对话框
- 显示选择省份的三种列表对话框-列表对话框,单选列表对话框,多选列表对话框
- 对话框
- 对话框
- 对话框
- 对话框
- 对话框
- 对话框
- 对话框
- 对话框
- 对话框
- 对话框
- 最大子数组II
- 【11.7】c++ primer plus 课后编程答案
- hdu 4004
- Java实现-最大子数组1
- mysql 主键自增 但是报主键重复的异常问题探究- Duplicate entry
- AlertDialog创建6种对话框的用法 (对话框,单选对话框,多选对话框,列表对话框,自定义对话框,退出对话框)
- Error setting expression 'orderid' with value ['2', ]
- 阿里云远程连接服务器oracle数据库
- Android 自定义View基础 onMeasure & onLayout
- 字符串的反转
- python和js
- Django 使用过程出现异常错误
- 6月4日,每日20行。
- R语言(递归分割树[传统决策树])分类模型(二)