7种形式的Android Dialog使用实例

2019-12-10 19:13:06于海丽

创建dialog的方法代码如下:

new AlertDialog.Builder(this).setTitle("列表框").setItems(
     new String[] { "Item1", "Item2" }, null).setNegativeButton(
     "确定", null).show();

7.信息内容是一个自定义的布局

7种形式的Android Dialog使用实例

dialog布局文件代码如下:

<?xml version="1.0" encoding="utf-8"?>
 
  <LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
   android:layout_height="wrap_content" android:layout_width="wrap_content"
   android:background="#ffffffff" android:orientation="horizontal"
   android:id="@+id/dialog">
   <TextView android:layout_height="wrap_content"
    android:layout_width="wrap_content"
   android:id="@+id/tvname" android:text="姓名:" />
   <EditText android:layout_height="wrap_content"
   android:layout_width="wrap_content" android:id="@+id/etname" android:minWidth="100dip"/>
  </LinearLayout>

创建dialog方法的代码如下: 

LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.dialog,
     (ViewGroup) findViewById(R.id.dialog));
    new AlertDialog.Builder(this).setTitle("自定义布局").setView(layout)
     .setPositiveButton("确定", null)
     .setNegativeButton("取消", null).show();