有两个例子,代码ppt上都有。
自己新建工程,默认是relativeLayout,改成LinearLayout,第一例子xml不用管,就写MainActivity.java就行
package com.example.dialog_test;
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);//上下文内容
dlg.setTitle("警示对话框");
dlg.setMessage("这里是警示内容");
// dlg.setIcon();这个自己加icon图片
//new DialogInterface.OnClickListener() 需要自己写对按钮的响应,也可以是null
dlg.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_LONG)
.show();
}
});
dlg.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_LONG)
.show();
}
});
dlg.setNeutralButton("查看详情", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, String.valueOf(arg1),
Toast.LENGTH_LONG).show();
}
});
dlg.create();
dlg.show();```
第二个例子
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
dlg.setTitle("警示对话框");
//dlg.setMessage("这里是警示内容");
// dlg.setIcon();
//需要自己写对按钮的响应
dlg.setSingleChoiceItems(R.array.gongsi, 0, new //R.array.gongsi是在values里的Strings.xml里写
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int which) {
// TODO Auto-generated method stub
String strMsg = "选中了"+String.valueOf(which);
Toast.makeText(MainActivity.this, strMsg, Toast.LENGTH_LONG).show();
}
});
dlg.create();
dlg.show();```
网友评论