使用AlertDialog创建对话框
(1)创建AlertDialog.Builder对象
(2)调用AlertDialog.Builder的setTitle()或setCustomTitle()设置标题,setIcon设置图片
(3)调用AlertDialog.Builder的相关方法设置对话框内容
setMessage():简单文本内容
setItems():简单列表项
setSingleChoiceItems():内容为单选列表
setMultiChoiceItems():内容为多选列表项
setAdapter():自定义列表项
setView():自定义View
(4)调用AlertDialog.Builder的setPositiveButton(),setNegativeButton()或setNeutralButton()添加多个按钮
(5)调用AlertDialog.Builder的create()方法创建AlertDialog对象,再用AlertDialog的show()将对话框显示出来
例子:
package com.example.maiyu.my1;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private Button register,register1,register2,register3,register4,register5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
register2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this , SimpleAlertDialog.class);
startActivity(intent);
}
});
}
private void initView() {
register = (Button)findViewById(R.id.register);
register1 = (Button)findViewById(R.id.register1);
register2 = (Button)findViewById(R.id.register2);
register3 = (Button)findViewById(R.id.register3);
register4 = (Button)findViewById(R.id.register4);
register5 = (Button)findViewById(R.id.register5);
}
//(2)这是注册1的实现方法
public void Second1(View v){
//通过intent来启动第二个类
Intent intent = new Intent();
//调用intent的setClass方法,这里传入两个参数,当期类.this , 启动的类.class
intent.setClass(MainActivity.this , SimpleAlertDialog.class);
//调用startActivity(意图对象)启动
startActivity(intent);
}
//(1)以下是注册按钮的实现方法
public void Second(View source){
//(1)创建AlertDialog.Builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//(2)设置对话框标题和图标
builder.setTitle("注册界面");
builder.setIcon(R.drawable.tools);
//设置对话框的测试内容
builder.setMessage("你真是个萨比,\n这难道不是真的?\n没错,就是");
//(3)为AlertDialog.Builder添加按钮
setPositiveButton(builder);
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this , "您取消了啦啦!" , Toast.LENGTH_SHORT).show();
}
});
//(4)调用AlertDialog.Builder的create()创建AlertDialog对象,再调用AlertDialog对象的show()方法
//将对话框显示出来
builder.create();
builder.show();
}
private AlertDialog.Builder setPositiveButton(final AlertDialog.Builder builder){
//调用setPositiveButton为方法添加”确定“按钮
return builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
register.setText("你点击了确定");
}
});
}
}
package com.example.maiyu.my1;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by maiyu on 2016/4/17.
*/
public class SimpleAlertDialog extends Activity {
TextView textView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
//setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView1);
simple();
}
//根据传入的View对象来创建对话框
private void simple() {
//(1)创建AlertDialog.Builder对象
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//(2)设置对话框标题和图标
builder.setTitle("简单列表对话框");
builder.setIcon(R.drawable.tools);
//设置简单的列表项内容
final String[] items = new String[]{"疯狂", "白痴" ,"你妹"};
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
textView.setText("你选中了<<" + items[which] + ">>");
}
});
//(3)为AlertDialog.Builder添加按钮
setPositiveButton(builder);
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
//(4)调用AlertDialog.Builder的create()创建AlertDialog对象,再调用AlertDialog对象的show()方法
//将对话框显示出来
builder.create();
builder.show();
}
private AlertDialog.Builder setPositiveButton(final AlertDialog.Builder builder){
//调用setPositiveButton为方法添加”确定“按钮
return builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
}
}
解释:
在AndroidManifest注册:
<activity
android:name=".SimpleAlertDialog"></activity>
这里:
用三种方式为按钮添加监听器:
(1)在XML中增加:
android:onClick="Second"
当我们按下按钮Second时,就会调用方法public void Second();
(2)同理,不过在方法Second1()中通过意图启动另一个类,需注册
(3)直接设置监听器,不调用android:onClick属性,
另外:
确定按钮和取消按钮这里也用不同方式,为了更理解