AlertDialog对话框
对话框在android开发中应用特别广AlertDialog对话框功能十分强大能创建出各种各样的对话框
创建一个AlertDialog对话框的基本步骤:
a.创建AlertDialog.Builder对象
b.设置标题setTitle()/setCustomTitle()
c.设置图标setIcon()
d.设置对话框内容
e.增加按钮
d.创建AlertDialog对象并展示出来
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.app.AlertDialog;
import android.view.MotionEvent;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
showAlertDialog();
return super.onTouchEvent(event);
}
private void showAlertDialog() {
AlertDialog.Builder ad = new AlertDialog.Builder(MainActivity.this)
.setTitle("我的对话框")
.setIcon(R.drawable.ic_launcher)
.setMessage("测试内容\n哈哈");
setPositiveButton(ad);
setNegativeButton(ad);
ad.create();
ad.show();
}
private AlertDialog.Builder setNegativeButton(AlertDialog.Builder ad) {
return ad.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
}
private AlertDialog.Builder setPositiveButton(AlertDialog.Builder ad) {
return ad.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
}
}