您的位置:首页 > 移动开发 > Android开发

Android—对话框

2016-05-12 16:25 417 查看
layout文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.testapp2.TestActivity5"
android:orientation="vertical">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="一般对话框"
android:onClick="bt1_onClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="bt2_onClick"
android:text="单选对话框"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="bt3_onClick"
android:text="复选对话框"/>
</LinearLayout>


java类代码:

package com.hanqi.testapp2;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class TestActivity5 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test5);
}
//一般对话框
public void bt1_onClick(View v)
{
//对话框不能直接实例化
//内部提供了构造器
//方法链调用
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("确认对话框")
.setMessage("确实要删除么")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(TestActivity5.this, "执行删除,which = " + which, Toast.LENGTH_SHORT).show();
}
})//正面按钮
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(TestActivity5.this, "取消删除,which = " + which, Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("中立", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(TestActivity5.this, "按钮,which = " + which, Toast.LENGTH_SHORT).show();
}
}).setCancelable(false)
.show();//显示对话框
}
//单选对话框
public void bt2_onClick(View v)
{
//final可以让常量的生命周期延长到整个实例
final String [] str = {"男","女"};
new AlertDialog.Builder(this)
.setTitle("单选对话框")
.setSingleChoiceItems(str, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(TestActivity5.this, "which = "+which+
",选中的是"+str[which], Toast.LENGTH_SHORT).show();
//关闭对话框
dialog.dismiss();
}
}).setCancelable(false)
.show();
}
//复选对话框
public void bt3_onClick(View v)
{
final String [] str = {"宝马","奔驰","劳斯莱斯","宾利"};
final boolean[] ch = {true,false,false,true};
new AlertDialog.Builder(this)
.setTitle("复选对话框")
.setMultiChoiceItems(str, ch, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//改变对应数组项的选中状态
ch[which] = isChecked;
}
})
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int i = 0;
//获取最终的选中状态
for (boolean b:ch)
{
Toast.makeText(TestActivity5.this, str[i]+"选中状态 = "+
(b?"选中":"未选中"), Toast.LENGTH_SHORT).show();
i++;
}
}
})
.setNegativeButton("取消", null)
.setCancelable(false)
.show();
}
}


效果图:







内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: