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();
   }
  });
 }
}
userinfo.xml

<?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="

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()