android通知对话框、多选对话框、单选对话框

来源:互联网 发布:sql distinct 数量 编辑:程序博客网 时间:2024/06/09 18:48

android通知对话框、多选对话框、单选对话框

在进行表单提交之前,一定会有用户的输入,输入就免不了一些提示和选择。本文就介绍一下通知对话框,多选对话框和单选对话框。
下面是主要代码:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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.example.wuxueyou.myfirstandroidapplication.MainActivity">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="多选对话框"        android:id="@+id/button1"        android:layout_centerVertical="true"        android:layout_centerHorizontal="true" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="提示框"        android:id="@+id/button"        android:layout_above="@+id/button1"        android:layout_centerHorizontal="true"        android:layout_marginBottom="68dp" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="单选dialog"        android:id="@+id/button2"        android:layout_below="@+id/button1"        android:layout_alignLeft="@+id/button1"        android:layout_alignStart="@+id/button1"        android:layout_marginTop="61dp" /></RelativeLayout>

package com.example.wuxueyou.myfirstandroidapplication;import android.content.DialogInterface;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    private final String[] hobbies= new String[]{"看书","编程","看电影"};    private boolean[] hobbiesChecked = new boolean[]{false,true,false};    private int hobbySingleIndex = 0;    private int hobbySingleIndextemp = 0;    private final String TAG = "MainActivity";    private Button dialogButton;    private Button listDialogButton;    private Button singlelistDialogButton;    private AlertDialog.Builder builder;    private AlertDialog.Builder mutilChoicebuilder;    private AlertDialog.Builder singleChoicebuilder;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        dialogButton = (Button) this.findViewById(R.id.button);        listDialogButton = (Button) this.findViewById(R.id.button1);        singlelistDialogButton = (Button)this.findViewById(R.id.button2);        //提示框        builder = new AlertDialog.Builder(this);        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 dialog, int which) {                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();            }        });        builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                Toast.makeText(MainActivity.this, "忽略", Toast.LENGTH_SHORT).show();            }        });        dialogButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                builder.show();            }        });        //多选框        mutilChoicebuilder = new AlertDialog.Builder(this);        mutilChoicebuilder.setTitle("多选框");        mutilChoicebuilder.setMultiChoiceItems(hobbies, hobbiesChecked, new DialogInterface.OnMultiChoiceClickListener() {            @Override            public void onClick(DialogInterface dialog, int which, boolean isChecked) {                if(isChecked){                    Toast.makeText(MainActivity.this, which + "-->" + hobbies[which], Toast.LENGTH_SHORT).show();                }            }        });        mutilChoicebuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                Log.i(TAG,which + "");            }        });        mutilChoicebuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                Log.i(TAG,which + "");            }        });        listDialogButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                mutilChoicebuilder.show();            }        });        //单选框        singleChoicebuilder = new AlertDialog.Builder(this);        singleChoicebuilder.setTitle("单选框");        singleChoicebuilder.setSingleChoiceItems(hobbies, 0, new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                Toast.makeText(MainActivity.this, which + "-->" + hobbies[which], Toast.LENGTH_SHORT).show();                hobbySingleIndextemp = which;            }        });        //确定和取消中的which代表的是点击的是哪个按钮,不代表对应的选中序号-->DialogInterface.BUTTON_POSITIVE        singleChoicebuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                hobbySingleIndex = hobbySingleIndextemp;                Log.i(TAG,which + "  ");            }        });        //确定和取消中的which代表的是点击的是哪个按钮,不代表对应的选中序号-->DialogInterface.BUTTON_NEGATIVE        singleChoicebuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                hobbySingleIndextemp = hobbySingleIndex;                Log.i(TAG,which + "  ");            }        });        singlelistDialogButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                singleChoicebuilder.show();            }        });    }}

运行效果:



0 0