android-弹出窗口的使用(1)

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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值