2012-02-18 09:55 android信息提示框与对话框 转载的,看着方便

来源:互联网 发布:怎样成为淘宝卖家 编辑:程序博客网 时间:2024/05/16 14:16
2012-02-18 09:55

android信息提示框与对话框 转载的,看着方便

某些情况下需要向用户弹出提示消息,如显示错误信息,收到短消息等,Android提供两种弹出消息的方式,消息提示框toasts和对话框alerts。

Toast是一种短暂的消息提示,显示一段时间后不需要用户交互会自动消失,所以用来显示一些建议性的不太重要的消息,如提示用户后台一个任务完成了。

使用Toast来弹出提示消息也很简单,调用Toast类的静态方法makeText():

 public static Toast makeText (Context context, CharSequence text, int duration)

context: 调用的上下文,通常为Application或Activity对象

text: 显示的消息

duration: 显示的时间长短,为 Toast.LENGTH_LONG或Toast.LENGTH_SHORT

AlertDialog类似于传统的模式对话框,需要与用户交互后才会关闭。

最简单的创建AlertDialog对话框的方法是使用AlertDialog的嵌套类Builder,它有下面几个主要的方法:

setMessage(): 设置显示的消息内容

setTitle() 和setIcon(): 设置对话框的标题栏的文字和图标

setPositiveButton(), setNeutralButton()和setNegativeButton(): 设置对话框的按钮,包括按钮显示的文字,按钮点击的事件

setView(): 设置对话框显示一个自定义的视图

 

1.在测试时,如何实现一个提示

可以使用

Toast.makeText(this, "这是一个提示", Toast.LENGTH_SHORT).show(); 
//从资源文件string.xml 里面取提示信息
  Toast.makeText(this, getString(R.string.welcome), Toast.LENGTH_SHORT).show();
这个提示会几秒钟后消失

 

2.可以使用AlertDialog.Builder 才产生一个提示框.

   例如像messagebox那样的

   new AlertDialog.Builder(this)
    .setTitle("Android 提示")
    .setMessage("这是一个提示,请确定")
    .show();
带一个确定的对话框

new AlertDialog.Builder(this)
         .setMessage("这是第二个提示")
         .setPositiveButton("确定",
                         new DialogInterface.OnClickListener(){
                                 public void onClick(DialogInterface dialoginterface, int i){
                                     //按钮事件
                                 }
                         })
         .show();

AlertDialog.Builder 还有很多复杂的用法,有确定和取消的对话框

new AlertDialog.Builder(this)
        .setTitle("提示")
        .setMessage("确定退出?")
        .setIcon(R.drawable.quit)
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
        setResult(RESULT_OK);//确定按钮事件
        finish();
        }
        })
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
         //取消按钮事件
        }
        })
        .show();

 

实例代码如下

public class AlertDialogBuilderSample extends Activity {
 View myView;
 EditText passWord, userName;

 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.test);

  LayoutInflater factory = LayoutInflater.from(this);
  myView = factory.inflate(R.layout.userinfo, null);// 获取布局文件;
  passWord = (EditText) myView.findViewById(R.id.password);
  // 获取自定义布局文件中的用户名编辑框组件
  userName = (EditText) myView.findViewById(R.id.usernameEdit);
  // 获取自定义文件中的密码编辑框组件

  final Button btnQuit = (Button) findViewById(R.id.button1);
  btnQuit.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    new AlertDialog.Builder(AlertDialogBuilderSample.this).setView(
      myView).setTitle("Question").setMessage(
      "自定义弹出框例子").setIcon(
      R.drawable.icon).setPositiveButton("Yes",
      new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog,
         int whichButton) {
        setResult(RESULT_OK);
        finish();
       }
      }).setNegativeButton("No",
      new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog,
         int whichButton) {
        Log.v("tag","pws"+passWord.getText());
        Log.v("tag","name"+userName.getText());
       }
      }).show();
   }
  });
  final Button btnTravels = (Button) findViewById(R.id.button2);
  btnTravels.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    new AlertDialog.Builder(AlertDialogBuilderSample.this)
      .setTitle("I want to go to").setItems(
        R.array.items_indide_dialog,
        new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog,
           int whichcountry) {
          String[] travelcountries = getResources()
            .getStringArray(
              R.array.items_indide_dialog);
          new AlertDialog.Builder(
            AlertDialogBuilderSample.this)
            .setMessage(
              "I’m going to "
                + travelcountries[whichcountry])
            .setNeutralButton(
              "Cancel",
              new DialogInterface.OnClickListener() {
               public void onClick(
                 DialogInterface dialog,
                 int whichButton) {
               }
              }).show();
         }
        }).show();
   }
  });
 }
}

 

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
 <LinearLayout
     android:id="@+id/usernamelayout"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:layout_marginTop="10dip">
  <TextView
      android:id="@+id/usernameText"
      android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:gravity="center"
   android:layout_centerInParent="true"
   android:layout_weight="3"
   android:text="用户名"/>
  <EditText
      android:id="@+id/usernameEdit"
      android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:maxLength="20"/>
 </LinearLayout>
 
 <!-- 地区 -->
 <LinearLayout
     android:id="@+id/arealayout"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:layout_below="@+id/usernamelayout">
  <TextView
      android:id="@+id/passwordTitle"
      android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:gravity="center"
   android:layout_centerInParent="true"
   android:layout_weight="3"
   android:text="密码"/>
  <EditText
      android:id="@+id/password"
      android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:maxLength="20"/>
 </LinearLayout>
</RelativeLayout>