Android开发中提示框经常使用,今天看着API,将一些经常用到的提示形式总结了一下,仅供参考!
转发请注明作者和出处:大飞_Rflyee-http://blog.csdn.net/rflyee/article/details/8978100
先了解一下AlertDialog.Builder创建对话框的几个常用方法:
setTitle() :设置标题
setIcon() :设置图标
setMessage():设置提示内容
setView() : 设置自定义视图
setItems() :设置对话框要显示的一个list
setMultiChoiceItems() :设置对话框显示的复选框
setNeutralButton() :普通按钮
setNegativeButton() :添加"No"按钮
create() : 创建对话框
show() :显示对话框
下面以一个例子简单演示一下其应用,先看一下效果图:
Toast的使用效果:
AlertDialog的简单使用:
类似于ListView的效果:
类似于RadioButton的效果:
多选的效果:
自定义视图:
代码示例如下:
一个主activity,两个布局文件;
ReviewCriteria.java
package com.test.restaurantfinder;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ReviewCriteria extends Activity {
private static final String tag = "reviewCriteria";
private final String arrayFruit[] = new String[]{"apple","banana","pear"};
private Button tishiButton = null; //提示Toast的按钮
private Button tishiAlert = null; //普通AlertDialog
private Button tishiAlertItem = null; //类似ListItem
private Button AlertRadio = null; //类似RadioButton
private int selectedFruitIndex = 0;
private Button CheckButton = null; //类似CheckBox
final boolean[] arrayFruitSelected = new boolean[] {false, true, false}; //默认选中
private Button myCustomButton = null; //自定义视图
private View myCustomView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_review_criteria);
this.tishiButton = (Button)findViewById(R.id.tishiButton);
this.tishiAlert = (Button)findViewById(R.id.tishiAlart);
this.tishiAlertItem = (Button)findViewById(R.id.AlertItem);
this.AlertRadio = (Button)findViewById(R.id.AlertSingleChoice);
this.CheckButton = (Button)findViewById(R.id.AlertCheckBox);
this.myCustomButton = (Button)findViewById(R.id.customView);
//Toast
this.tishiButton.setOnClickListener(
new OnClickListener(){
@Override
public void onClick(View v){
Log.i(tag, "in onClick1");
/*注:在内嵌函数中使用时context 一定要对,不能只是this,而应该是Class.this或者getApplicationContext()*/
//Toast.makeText(ReviewCriteria.this, "this is a toast", Toast.LENGTH_LONG).show();
Toast toast = Toast.makeText(ReviewCriteria.this, "this is a toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});
//AlertDialog
this.tishiAlert.setOnClickListener(
new OnClickListener(){
public void onClick(View v){
new AlertDialog.Builder(ReviewCriteria.this).
setIcon(R.drawable.ic_launcher).//图标
setTitle("Delete?").//标题
setMessage("this is a AlertDialog!").//提示内容
setPositiveButton("Yes", new DialogInterface.OnClickListener() {//确定
@Override
public void onClick(DialogInterface arg0, int arg1) {
//yes to do
}
}).setNegativeButton("No", new DialogInterface.OnClickListener(){//取消
@Override
public void onClick(DialogInterface arg1,int witch){
//no to do
}
}).
setNeutralButton("More", new DialogInterface.OnClickListener() {//普通确定按钮
@Override
public void onClick(DialogInterface dialog, int which) {
//查看更多
}
}).
show();
}
});
//类似ListItem
this.tishiAlertItem.setOnClickListener(
new OnClickListener(){
public void onClick(View v){
new AlertDialog.Builder(ReviewCriteria.this).
setTitle("witch do you like?").
setItems(arrayFruit,new DialogInterface.OnClickListener() {//Items to choose
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(ReviewCriteria.this, arrayFruit[which], Toast.LENGTH_LONG).show();
}
}).
setNegativeButton("cancel", new DialogInterface.OnClickListener() {//cancel
@Override
public void onClick(DialogInterface dialog, int which) {
//cancel to do
}
}).
show();
}
});
//类似RadioButton
this.AlertRadio.setOnClickListener(
new OnClickListener(){
public void onClick(View v){
new AlertDialog.Builder(ReviewCriteria.this).
setTitle("witch do you like?").
setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectedFruitIndex = which;
}
}).
setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(ReviewCriteria.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_LONG).show();
}
}).
setNegativeButton("Cancel", new DialogInterface.OnClickListener() {//cancel
@Override
public void onClick(DialogInterface dialog, int which) {
//cancel to do
}
}).
show();
}
});
//类似CheckBox
this.CheckButton.setOnClickListener(
new OnClickListener(){
public void onClick(View v){
new AlertDialog.Builder(ReviewCriteria.this).
setTitle("which do you like?").
setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface arg0, int which, boolean isChecked) {
arrayFruitSelected[which] = isChecked;
}
}).
setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String toastString = "你选中了:";
for(int i = 0;i < arrayFruit.length; i++){
if(arrayFruitSelected[i]){
toastString += arrayFruit[i]+"、";
}
}
Toast.makeText(ReviewCriteria.this, toastString, Toast.LENGTH_LONG).show();
}
}).
setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).
show();
}
});
//自定义视图
LayoutInflater layoutInflater = LayoutInflater.from(this);
myCustomView = layoutInflater.inflate(R.layout.customview, null);
this.myCustomButton.setOnClickListener(
new OnClickListener(){
public void onClick(View v){
new AlertDialog.Builder(ReviewCriteria.this).
setTitle("login").
setView(myCustomView).
setPositiveButton("Login", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).
setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).
show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.review_criteria, menu);
return true;
}
}
activity_review_criteria.xml
<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=".ReviewCriteria" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<!--show tips by Toast -->
<Button
android:id="@+id/tishiButton"
android:layout_below="@id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="show tips by Toast"/>
<!--show tips by AlertDialog -->
<Button
android:id="@+id/tishiAlart"
android:layout_below="@id/tishiButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="show tips by AlertDialog"/>
<!--AlertDialog : Item选择,类似ListView -->
<Button
android:id="@+id/AlertItem"
android:layout_below="@id/tishiAlart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="like ListView"/>
<!--AlertDialog : Item选择,类似RadioButton -->
<Button
android:id="@+id/AlertSingleChoice"
android:layout_below="@id/AlertItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Like RadioButton"/>
<!--AlertDialog : Item选择,类似CheckBox -->
<Button
android:id="@+id/AlertCheckBox"
android:layout_below="@id/AlertSingleChoice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Like CheckBox"/>
<!--AlertDialog : 自定义视图 -->
<Button
android:id="@+id/customView"
android:layout_below="@id/AlertCheckBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="CustomView"/>
</RelativeLayout>
customview.xml
<?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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/usernameTextView"
android:text="username:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/usernameEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/passwordTextView"
android:text="password:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/passwordEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>