自定义模态提示框

来源:互联网 发布:sql字段拼接 编辑:程序博客网 时间:2024/06/06 09:57

关于Android中模态提示框的问题

因为项目最新的效果图,中提示框为模态的,先前用的Toast,虽然可以实现功能,但是提示框后面并没有模态框那样的灰色半透明蒙板,现把代码给大家参考

 customer_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- @author gKF55694 自定义dialog -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal" android:layout_width="wrap_content"
 android:paddingRight="20dip" android:paddingLeft="20dip"
 android:layout_height="wrap_content"   android:id="@+id/body" >
 <TextView android:id="@+id/txt_content" android:layout_width="wrap_content"
  android:gravity="left"
  android:layout_height="wrap_content" android:textSize="15dip" android:textColor="#FFFFFF"
  />
</LinearLayout>

 以下是核心的代码

public class Activity01 extends Activity {
 private int mCount = 0; // 提示框的计数
 AlertDialog al = null; // 提示框
 private int tempStep; // 停留秒数

 public void displayInfo(String content, int step) // 在调用的时候,只需要传入提示内容,和停留时间
 {
  mCount = 0;
  tempStep = step;
  LayoutInflater inflater = LayoutInflater.from(Activity01.this);
  View customerLayout = inflater.inflate(R.layout.customer_dialog, null);
  customerLayout.setLayoutParams(new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT,
    LinearLayout.LayoutParams.WRAP_CONTENT));
  TextView txtContent = (TextView) customerLayout
    .findViewById(R.id.txt_content);
  txtContent.setText(content);
  al = new AlertDialog.Builder(Activity01.this).setIcon(0)
    .setView(customerLayout).create();
  new Thread() {
   public void run() {
    try {
     while (mCount < tempStep) {
      Thread.sleep(1000);
      mCount++;
     }
     al.dismiss();
    } catch (Exception ex) {
     al.dismiss();
    }
   }
  }.start();
  al.show();
 }
}