标签:android android对话框
package com.example.androiddialog;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
private Button button7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button) super.findViewById(R.id.button1);
button2=(Button) super.findViewById(R.id.button2);
button3=(Button) super.findViewById(R.id.button3);
button4=(Button) super.findViewById(R.id.button4);
button5=(Button) super.findViewById(R.id.button5);
button6=(Button) super.findViewById(R.id.button6);
button7=(Button) super.findViewById(R.id.button7);
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setMessage("确认退出吗?");
builder.setTitle("提示");
builder.setPositiveButton("确认", new OnClickListener(){ //Positive:正,肯定
@Override
public void onClick(DialogInterface dialog, int which){
dialog.dismiss(); //解散、消除
MainActivity.this.finish();
}
});
builder.setNegativeButton("取消", new OnClickListener(){ //Negative:负,否定
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
});
效果图:
button2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Dialog dialog = new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.btn_star).setTitle("喜好调查").setMessage("你喜欢李连杰的电影吗?")
.setPositiveButton("很喜欢",new OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "我很喜欢他的电影。",Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("不喜欢", new OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "我不喜欢他的电影。", Toast.LENGTH_LONG).show();
}
}).setNeutralButton("一般", new OnClickListener(){ //Neutral:中立
@Override
public void onClick(DialogInterface dialog, int which){
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "谈不上喜欢不喜欢。", Toast.LENGTH_LONG).show();
}
}).create();
dialog.show();
}
});
效果图:
button3.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
final EditText et=new EditText(MainActivity.this);
new AlertDialog.Builder(MainActivity.this)
.setTitle("请输入")
.setIcon(android.R.drawable.ic_dialog_info)
.setView(et)
.setPositiveButton("确定",new OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"你输入的是:"+et.getText(),Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("取消", new OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "你选择的是取消",Toast.LENGTH_LONG).show();
}
}).show();
}
});
效果图:
button4.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
final String[] cities=new String[]{ "北京", "上海" };
new AlertDialog.Builder(MainActivity.this).setTitle("你喜欢的城市是:")
.setMultiChoiceItems(cities,null, null)
.setPositiveButton("确定",new OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
// TODO Auto-generated method stub
String str="";
ListView listView =((AlertDialog) dialog).getListView();
for (int i = 0;i<cities.length; i++) {
if (listView.getCheckedItemPositions().get(i)) {
str+=cities[i]+",";
}
}
str= str.substring(0,str.length()-1);
Toast.makeText(MainActivity.this,"你输入的是:"+str,Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("取消", new OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "你选择的是取消",Toast.LENGTH_LONG).show();
}
}).show();
}
});
效果图:
button5.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
final String[] cities=new String[]{ "北京", "上海" };
new AlertDialog.Builder(MainActivity.this).setTitle("中国的首都是:")
.setIcon(android.R.drawable.ic_dialog_info)
//.setSingleChoiceItems(cities,0, new DialogInterface.OnClickListener() {
//public void onClick(DialogInterface dialog, int which) {
//dialog.dismiss();
//}
//})
.setSingleChoiceItems(cities,0,null)
.setPositiveButton("确定",new OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
// TODO Auto-generated method stub
String str="";
ListView listView =((AlertDialog) dialog).getListView();
for (int i = 0;i<cities.length; i++) {
if (listView.getCheckedItemPositions().get(i)) {
str=cities[i];
break;
}
}
Toast.makeText(MainActivity.this,"你输入的是:"+str,Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("取消", new OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "你选择的是取消",Toast.LENGTH_LONG).show();
}
}).show();
}
});
效果图:
button6.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this).setTitle("您选择的城市是:")
.setItems(new String[] { "北京","上海","杭州"}, null)
.setPositiveButton("确定",new OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
Toast.makeText(MainActivity.this,"您选择的是确认",Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("取消", new OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
Toast.makeText(MainActivity.this, "你选择的是取消",Toast.LENGTH_LONG).show();
}
}).show();
}
});
效果图:
button7.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.user_info,null);
final EditText txt1=(EditText)layout.findViewById(R.id.editText1);
final EditText txt2=(EditText)layout.findViewById(R.id.editText2);
new AlertDialog.Builder(MainActivity.this).setTitle("用户登陆").setView(layout)
.setPositiveButton("确定",new OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
Toast.makeText(MainActivity.this,"用户名为:"+txt1.getText()+"\n密码为:"+txt2.getText(),Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("取消", new OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "你选择的是取消",Toast.LENGTH_LONG).show();
}
}).show();
}
});
}
user_info.xml
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_toRightOf="@+id/textView2"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="20dp"
android:text="用户名:" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_marginTop="37dp"
android:layout_toLeftOf="@+id/editText1"
android:text="密 码:" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_alignTop="@+id/textView1"
android:ems="10"
android:inputType="textPassword" />
</RelativeLayout>
</LinearLayout>
效果图:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
android各种对话框
标签:android android对话框
原文地址:http://blog.csdn.net/qfxsxhfy/article/details/45059165