andriod之对话框--标准对话框、列表对话框、自定义对话框

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">


    <Button
        android:id="@+id/btnStandardDialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="标准对话框" />
    <Button
        android:id="@+id/btnItemsDialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="列表对话框" />
    <Button
        android:id="@+id/btnCustomDialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="自定义对话框" />
    

</LinearLayout>

自定义对话框布局:custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="输入地址"/>
<EditText 
   android:id="@+id/etAddress"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"/>
</LinearLayout>

java类:

package com.sxt.day05_09;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListener();
}


private void setListener() {
setStandardDialogClickListener();
setItemsDialogClickListener();
setCustomDialogClickListener();
}


private void setCustomDialogClickListener() {
findViewById(R.id.btnCustomDialog).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//将custom_dialog.xml解析为一个View类型的对象
View layout = View.inflate(MainActivity.this, R.layout.custom_dialog, null);
final EditText etAddrss=(EditText) layout.findViewById(R.id.etAddress);
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("输入地址的对话框")
.setView(layout)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String address=etAddrss.getText().toString();
Log.i("main",address);
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}


//创建列表类型的对话框
private void setItemsDialogClickListener() {
findViewById(R.id.btnItemsDialog).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setItems(new String[]{"增加数据","删除数据","修改数据"}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
Toast.makeText(MainActivity.this, "执行增加数据的操作", 3000).show();
break;
case 1:
Toast.makeText(MainActivity.this, "执行删除数据的操作", 3000).show();
break;
case 2:
Toast.makeText(MainActivity.this, "执行修改数据的操作", 3000).show();
break;
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}


private void setStandardDialogClickListener() {
findViewById(R.id.btnStandardDialog).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//创建对话框的Builder对象
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("退出考试窗口")
.setMessage("退出考试吗?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("main","退出考试");
}
}).setNegativeButton("放弃", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("main","继续考试");
}
});
//创建对话框
AlertDialog dialog = builder.create();
dialog.show();
}
});
}


}

效果:

andriod之对话框--标准对话框、列表对话框、自定义对话框

andriod之对话框--标准对话框、列表对话框、自定义对话框

andriod之对话框--标准对话框、列表对话框、自定义对话框


你可能感兴趣的:(andriod之对话框--标准对话框、列表对话框、自定义对话框)