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
- android通知对话框、多选对话框、单选对话框
- Android通知之单选对话框通知
- Android-Dialog对话框 全解(普通对话框,单选对话框,多选对话框,列表对话框,自定义对话框,Activity对话框)
- Android简单对话框、输入对话框、单选对话框、复选对话框、列表对话框、自定义对话框、时间对话框
- android 单选对话框
- android,单选对话框,进度条对话框
- Android的普通对话框,单选和多选对话框
- AlertDialog创建6种对话框的用法 (对话框,单选对话框,多选对话框,列表对话框,自定义对话框,退出对话框)
- Android通知之多选对话框通知
- 安卓通知的使用系列4:对话框通知的使用之列表对话框、单选列表对话框、多选列表对话框
- Android学习(31) --对话框&单选对话框&多选对话框
- Android 实现单选对话框
- 各种对话框的实现(文字对话框,选择对话框,弹出输入对话框,单选,多选对话框,自定义对话框)
- Android基础控件——AlertDialog、ProgressDialog实现单选对话框、多选对话框、进度条对话框、输入框对话框
- Android对话框之多选对话框
- Android中的普通对话框、单选对话框、多选对话框、带Icon的对话框、以及自定义Adapter和自定义View对话框详解
- (转载)Android中的普通对话框、单选对话框、多选对话框、带Icon的对话框、以及自定义Adapter和自定义View对话框详解
- Android中的普通对话框、单选对话框、多选对话框、带Icon的对话框、以及自定义Adapter和自定义View对话框详解
- 删除链表中重复的结点
- sql语句 (中 基础)
- Object 对象方法 hasOwnProperty()
- Redis数据结构之字典
- linux下如何安装redis
- android通知对话框、多选对话框、单选对话框
- Plupload
- window<->linux移植
- 雪花飘落插件
- 多线程编程总结
- 顺序表应用4:元素位置互换之逆置算法
- centos下hadoop2.7.1伪分布式搭建
- ASP.NET多种不同页面间数据传递的方法
- htmlcss渐变及兼容性