java

liucheng

android 组件之 alertDialog

AlertDialog对话框为我们做重要操作时。弹出一个框,做确认信息提示。

第一。AlertDialog 的对话框提示、内容比较简单。就不解释了、

package com.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class AlertActivity extends Activity {
	private Button mButton1;
    private TextView testView;
    private ImageView imageView;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mButton1 = (Button) findViewById(R.id.myButton1);
	
		final Builder alert =    new AlertDialog.Builder(AlertActivity.this);
		mButton1.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View v) {
				 alert.setIcon(R.drawable.icon);
				 alert.setTitle(R.string.app_about);
				 alert.setMessage(R.string.app_about_msg);
				 alert.setPositiveButton(R.string.str_ok, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
				 alert.show();
				
			}
			
		});
	
		
	}
}

效果图:


第二:AlertDialog 创建列表对话框。

package com.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class AlertActivity extends Activity {
	private Button mButton1;
    private TextView testView;
    private ImageView imageView;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mButton1 = (Button) findViewById(R.id.myButton1);
	 
		
		final Builder alert =    new AlertDialog.Builder(AlertActivity.this);
		mButton1.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View v) {
				 alert.setIcon(R.drawable.icon);
				 alert.setTitle(R.string.app_about);
 
 				 alert.setItems(new String[]{"红色","绿色","蓝色"}, new OnClickListener() {
 					@Override
 					public void onClick(DialogInterface dialog, int which) {
 						System.out.println(which);
 					}
 					
 					 
 				});
			 
				 alert.show();
				
			}
			
		});
		 
	}
}

效果图:

第三。使用ALertDialog创建单选列表对话框。

只要AlertDialog.Builder 调用setSingleChoiceItems 就可以创建一个单选列表的对话框。

代码如下:

package com.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class AlertActivity extends Activity {
	private Button mButton1;
    private TextView testView;
    private ImageView imageView;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mButton1 = (Button) findViewById(R.id.myButton1);
		testView = (TextView) findViewById(R.id.str_res);
		imageView = (ImageView)findViewById(R.id.imageView);
		
		final Builder alert =    new AlertDialog.Builder(AlertActivity.this);
		mButton1.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View v) {
				 alert.setIcon(R.drawable.icon);
				 alert.setTitle(R.string.app_about);
 
				 alert.setSingleChoiceItems(new String[]{"红色","绿色","蓝色"}, 1, new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						System.out.println(which);
					}
				});
				 alert.setPositiveButton("确定", null);
 
				 alert.show();
				
			}
			
		});
		 
		
	}
}

效果图:


如果想给某个AlertDialog 只添加额外的页面。

TableLayout layout =(TableLayout)getLayoutInflater().inflate(R.layout.login_user_activity,null);
dialog.setView(layout);
效果图:




阅读更多
个人分类: android
想对作者说点什么? 我来说一句

alertDialog大全

2014年10月20日 234KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭