对话框简介
android提供了丰富的对话框支持,支持四种如下的对话框。
AlertDialog简介
介绍上面六个方法的代码示例
setMessage()
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/text02" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="setMessage"
android:id="@+id/button"
android:onClick="dialog"/>
</LinearLayout>
package peng.liu.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView text02;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text02 = (TextView) findViewById(R.id.text02);
Button setMessage = (Button) findViewById(R.id.button);
}
public void dialog(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)
.setTitle("dialog")
.setMessage("hello world");
setPositiveButton(builder);
setNegitiveButton(builder);
builder.create().show();
}
public void setPositiveButton(AlertDialog.Builder builder){
builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
text02.setText("单击了确定按钮");
}
});
}
public void setNegitiveButton(AlertDialog.Builder builder){
builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
text02.setText("单击了取消按钮");
}
});
}
}
简单列表对话框setItems()
需要传入一个数组或者数组的资源id
package peng.liu.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView text02;
private String[] items = new String[]{
"java","python","html","css"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text02 = (TextView) findViewById(R.id.text02);
Button setMessage = (Button) findViewById(R.id.button);
}
public void dialog(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)
.setTitle("dialog")
.setItems(items,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
text02.setText(items[i]);
}
});
setPositiveButton(builder);
setNegitiveButton(builder);
builder.create().show();
}
public void setPositiveButton(AlertDialog.Builder builder){
builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
text02.setText("单击了确定按钮");
}
});
}
public void setNegitiveButton(AlertDialog.Builder builder){
builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
text02.setText("单击了取消按钮");
}
});
}
}
单选列表对话框setSingleChooseItems
参数是数组,sursor,或者ListAdapter。
package peng.liu.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView text02;
private String[] items = new String[]{
"java","python","html","css"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text02 = (TextView) findViewById(R.id.text02);
Button setMessage = (Button) findViewById(R.id.button);
}
public void dialog(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)
.setTitle("dialog")
//1表示第二个框被选中
.setSingleChoiceItems(items,1,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
text02.setText(items[i]);
}
});
setPositiveButton(builder);
setNegitiveButton(builder);
builder.create().show();
}
public void setPositiveButton(AlertDialog.Builder builder){
builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
text02.setText("单击了确定按钮");
}
});
}
public void setNegitiveButton(AlertDialog.Builder builder){
builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
text02.setText("单击了取消按钮");
}
});
}
}
多选列表对话框 setMultiChooseItems
参数是数组或者cursor
package peng.liu.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView text02;
private String[] items = new String[]{
"java","python","html","css"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text02 = (TextView) findViewById(R.id.text02);
Button setMessage = (Button) findViewById(R.id.button);
}
public void dialog(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)
.setTitle("dialog")
//布尔数组表示第二个和第四个被选中
.setMultiChoiceItems(items,new boolean[]{false,true,false,true},null);
setPositiveButton(builder);
setNegitiveButton(builder);
builder.create().show();
}
public void setPositiveButton(AlertDialog.Builder builder){
builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
text02.setText("单击了确定按钮");
}
});
}
public void setNegitiveButton(AlertDialog.Builder builder){
builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
text02.setText("单击了取消按钮");
}
});
}
}
自定义列表项对话框 setAdapter
参数是Adapter,该方法和setSingleChooseItems都可以接受Adapter作为参数。
package peng.liu.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView text02;
private String[] items = new String[]{
"java","python","html","css"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text02 = (TextView) findViewById(R.id.text02);
Button setMessage = (Button) findViewById(R.id.button);
}
public void dialog(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)
.setTitle("dialog")
//布尔数组表示第二个和第四个被选中
.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items),null);
setPositiveButton(builder);
setNegitiveButton(builder);
builder.create().show();
}
public void setPositiveButton(AlertDialog.Builder builder){
builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
text02.setText("单击了确定按钮");
}
});
}
public void setNegitiveButton(AlertDialog.Builder builder){
builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
text02.setText("单击了取消按钮");
}
});
}
}
自定义View对话框
自定义的任何的View组件
package peng.liu.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView text02;
private String[] items = new String[]{
"java","python","html","css"
};
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text02 = (TextView) findViewById(R.id.text02);
Button setMessage = (Button) findViewById(R.id.button);
image = new ImageView(this);
image.setImageResource(R.drawable.ic_launcher);
image.setScaleType(ImageView.ScaleType.FIT_XY);
image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
public void dialog(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)
.setTitle("dialog")
//布尔数组表示第二个和第四个被选中
.setView(image);
}
public void setPositiveButton(AlertDialog.Builder builder){
builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
text02.setText("单击了确定按钮");
}
});
}
public void setNegitiveButton(AlertDialog.Builder builder){
builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
text02.setText("单击了取消按钮");
}
});
}
}
时间: 2024-10-11 16:06:05