Android入门/AlertDialog(十)

1. 实现系统自带的AlertDialog 

2. 实现 自定义Layout的AlertDialog

3. 更改AlertDialog的Theme

先上效果图

Android入门/AlertDialog(十)_第1张图片Android入门/AlertDialog(十)_第2张图片

1. main.xml 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <Button 
        android:id="@+id/yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="使用Layout的对话框"/>
	<Button 
	    android:id="@+id/no"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="默认样式的对话框"/>
</LinearLayout>
2. 自定义AlertDialog的布局文件dialog.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:orientation="vertical">
	<EditText 
	    android:layout_height="wrap_content"
		android:layout_width="fill_parent" 
		android:layout_marginLeft="20dip"
		android:layout_marginRight="20dip" 
		android:textAppearance="?android:attr/textAppearanceMedium" 
		android:id="@+id/edtInput"/>
</LinearLayout>

android:textAppearance= "";

3. java代码

public class MainActivity extends Activity {

	private Button yes;
	private Button no;
    
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
         
        preInitUI();
        postInitUI();     
    }

    public void preInitUI()
    {
    	 setContentView(R.layout.activity_main);
    	 yes = (Button)findViewById(R.id.yes);
         no = (Button)findViewById(R.id.no);
    }
    
    public void postInitUI()
    {
    	  yes.setOnClickListener(listener);
          no.setOnClickListener(listener);
    }
    
    private OnClickListener listener = new OnClickListener() {
		
		public void onClick(View v) {
				switch (v.getId()) {
				case R.id.no:
					showDialog(MainActivity.this);
					break;

				case R.id.yes:
					showDialog_Layout(MainActivity.this);
					break;
				}
		}
	};
	// 显示基本的Dialog
	public void showDialog(Context context){
		// 1. 创建一个builder对象
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		// 2.set 相关的值 或属性
		builder.setIcon(R.drawable.xuehua);
	    builder.setTitle("Title");  
	    builder.setMessage("Message");  
	    builder.setPositiveButton("Name A", new DialogInterface.OnClickListener()
	    {
			public void onClick(DialogInterface dialog, int which) {
				 setTitle("点击了对话框上的Name A");  
			}
	    });
	    builder.setNeutralButton("Name B", new DialogInterface.OnClickListener()
	    {
			public void onClick(DialogInterface dialog, int which) {
				 setTitle("点击了对话框上的Name B");  
			}
	    });
	    builder.setNegativeButton("Name C", new DialogInterface.OnClickListener()
	    {
			public void onClick(DialogInterface dialog, int which) {
				 setTitle("点击了对话框上的Name C");  
			}
	    });
	    // 展示 
	    builder.show();
	}
    
	
	public void showDialog_Layout(Context context){
			
		  LayoutInflater inflater =  (LayoutInflater) context
			.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
 
		  // 绑定 布局 
		  View view = inflater.inflate(R.layout.dialog, null);
		  		//到绑定布局内的 子控件
		  		final EditText edtInput=(EditText)view.findViewById(R.id.edtInput);  
		  
		  AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AppTheme));
		  
		  
		    builder.setCancelable(false);//设置为false,按返回键不能退出。默认为true。  
	        builder.setIcon(R.drawable.hong);  
	        builder.setTitle("Title");  
	        builder.setView(view);  
	        builder.setPositiveButton("确认",new DialogInterface.OnClickListener() {  
	                    public void onClick(DialogInterface dialog, int whichButton) {  
	                        setTitle(edtInput.getText());  
	                    }  
	        });  
	        builder.setNegativeButton("取消",  
	                new DialogInterface.OnClickListener() {  
	                    public void onClick(DialogInterface dialog, int whichButton) {  
	                       finish();
	                    }  
	        });  
	        builder.show();  
	}    
}





你可能感兴趣的:(Android入门/AlertDialog(十))