android几种常见弹出窗口实现如下:

public class AndroidLearn extends Activity {

EditText pwdText ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_learn);

/*1、不带输入框的弹出窗口,可用于提醒等功能*/
Button btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder dialog = new AlertDialog.Builder(AndroidLearn.this);
//dialog.setIcon(R.drawable.ic_launcher);//窗口头图标
dialog.setTitle("提示");//窗口名
dialog.setMessage("是否退出应用程序 ");
dialog.setPositiveButton("确定",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}
});
dialog.setNegativeButton("取消",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}
});
//dialog.setNeutralButton("中间按钮",new DialogInterface.OnClickListener() {
//public void onClick(DialogInterface dialog, int which) {
//// TODO Auto-generated method stub
//
//}
//});
dialog.show();
}
});

/*2、带简单输入框的弹出对话框*/
Button btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

final EditText edit = new EditText(AndroidLearn.this);
AlertDialog.Builder dialog = new AlertDialog.Builder(AndroidLearn.this);
//dialog.setIcon(R.drawable.ic_launcher);//窗口头图标
dialog.setTitle("请输入密码");//窗口名
dialog.setView(edit);

dialog.setPositiveButton("确定",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
System.out.println(edit.getText().toString());
}
});
dialog.setNegativeButton("取消",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
dialog.show();
}
});


/*3、自定义弹出对话框*/
Button btn3 = (Button) findViewById(R.id.button3);
btn3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LayoutInflater flater = getLayoutInflater();
View view = flater.inflate(R.layout.self_layout, (ViewGroup) findViewById(R.id.dialog));
final EditText name = (EditText) view.findViewById(R.id.name);
final EditText pwd = (EditText) view.findViewById(R.id.password);
AlertDialog.Builder dialog = new AlertDialog.Builder(AndroidLearn.this);
dialog.setIcon(R.drawable.ic_launcher);//窗口头图标
dialog.setTitle("自定义对话框");//窗口名
dialog.setMessage("这是简单例子");//窗口信息
dialog.setView(view);

dialog.setPositiveButton("确定",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

System.out.println("enter name =="+name.getText().toString());
System.out.println("enter pwd =="+pwd.getText().toString());

}
});
dialog.setNegativeButton("取消",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
dialog.show();
}
});

/*4、单选弹出对话框*/
Button btn4 = (Button) findViewById(R.id.button4);
btn4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

AlertDialog.Builder dialog = new AlertDialog.Builder(AndroidLearn.this);
dialog.setIcon(android.R.drawable.ic_dialog_info);//窗口头图标
dialog.setTitle("单选框");//窗口名
dialog.setSingleChoiceItems(new String[]{"Item1", "Item2"}, 0,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
System.out.println("选择第"+which+"个");
}} );

dialog.setNegativeButton("取消", null);
dialog.show();
}
});

/*4、多选弹出对话框*/
Button btn5 = (Button) findViewById(R.id.button5);
btn5.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder dialog = new AlertDialog.Builder(AndroidLearn.this);
dialog.setIcon(android.R.drawable.ic_dialog_info);//窗口头图标
dialog.setTitle("多选框");//窗口名
dialog.setMultiChoiceItems(new String[]{"Item1", "Item2"},new boolean[]{false,false} ,new DialogInterface.OnMultiChoiceClickListener() {

@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub

}
});
dialog.setNegativeButton("取消", null);
dialog.show();

}
});
}
}

三种布局文件如下:

activity_android_learn.xml文件如下:

<RelativeLayout 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"
tools:context=".AndroidLearn" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="简单对话框" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:text="简单输入框的弹出对话框" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/button2"
android:layout_below="@+id/button2"
android:text="自定义对话框" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/button2"
android:layout_below="@+id/button3"
android:text="单选对话框" />

<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/button2"
android:layout_below="@+id/button4"
android:text="多选对话框" />
</RelativeLayout>

dialog.xml布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="#ffffffff" android:orientation="horizontal"
android:id="@+id/dialog">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/tvname"
android:text="姓名:" />
<EditText
android:id="@+id/etname"
android:layout_width="278dp"
android:layout_height="wrap_content"
android:minWidth="100dip" />
</LinearLayout>

self_layout.xml布局文件如下:

<?xml version="1.0" encoding="UTF-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/back"

android:orientation="vertical">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="none" >
</EditText>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="none" >
</EditText>
</LinearLayout>

附上源码备份:http://download.csdn.net/detail/sunnyfans/5163240




更多相关文章

  1. Android在Service中弹出对话框(Dialog),即全局性对话框
  2. Android:创建常见对话框以及使用对话框实现登陆
  3. 如何让按钮共享android中对话框宽度的一半?
  4. Android多窗口分屏(原生方法)
  5. Java多线程聊天对话框
  6. JavaFX窗口自适应
  7. java 画图注意 改变窗口大小不会消失
  8. Javascript 无提示框关闭IE窗口

随机推荐

  1. Android 基础 源码 工具
  2. Android 呼吸灯流程分析(一)
  3. android中校验email是否合法
  4. Android中设置控件透明度的方法
  5. 封装的一个android底部操作弹出窗
  6. Android文档阅读03—开发工具
  7. [置顶] Android修改源代码控制不锁屏
  8. android 系统状态栏的隐藏和显示
  9. Android 之 快速开发框架 afinal
  10. android layout_gravity 和gravity