android 自己封装自己所需的Dialog对话框

有时候项目中需要封装自己所需的Dialog对话框。

自己写Dialog的layout布局文件

布局文件layout_dialog.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp" >

    <TextView
        android:id="@+id/Manual_LastRead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" 
        android:text="测试Dialog"/>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="姓名:"
            android:textSize="14sp" />

        <EditText
            android:id="@+id/name_Input"
            android:layout_width="230dp"
            android:layout_height="45dp"
            android:textSize="14sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="年龄"
            android:textSize="14sp" />

        <EditText
            android:id="@+id/old_Input"
            android:layout_width="230dp"
            android:layout_height="45dp"
            android:digits="0123456789"
            android:inputType="number"
            android:textSize="14sp" />
    </LinearLayout>

</LinearLayout>
 TestDialogActivity
public class TestDialogActivity extends Activity {
	
	public final static int DIALOG_MESSAGE = 0;
	
	private TextView mTextResult;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test_dialog);
		
		mTextResult = (TextView) findViewById(R.id.test_result);
		findViewById(R.id.test_dialog).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showDialog(DIALOG_MESSAGE);
			}
		});
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
			case DIALOG_MESSAGE:
				LayoutInflater factory = LayoutInflater.from(this);
				final LinearLayout rView = (LinearLayout)factory.inflate(R.layout.layout_dialog, null);
				final EditText nameEdit = (EditText)rView.findViewById(R.id.name_Input);
				final EditText oldEdit = (EditText)rView.findViewById(R.id.old_Input);
				
				return new AlertDialog.Builder(this).setView(rView).setPositiveButton("确认", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						mTextResult.setText("姓名:" +nameEdit.getText().toString() + " 年龄:" + oldEdit.getText().toString());
					}
				}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
					}
				}).create();
				
		}
		
		return super.onCreateDialog(id);
	}
	

}
 
 

相关推荐