实现不选择选项则不可以关闭的对话框之方法三

本方法基于Dialog,使用自定义布局,更能满足发杂的需求。

布局文件:dialog_must_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/activity_horizontal_margin" >

    <TextView
        android:id="@+id/txtUsername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/edtUsername"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="姓名: " />

    <EditText
        android:id="@+id/edtUsername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/txtUsername"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/txtHorizontalCenter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/btnOK"
        android:layout_below="@+id/edtUsername"
        android:layout_centerHorizontal="true"
        android:text="" />

    <Button
        android:id="@+id/btnOK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edtUsername"
        android:layout_toLeftOf="@+id/txtHorizontalCenter"
        android:text="确定" />

    <Button
        android:id="@+id/btnCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/txtHorizontalCenter"
        android:layout_toRightOf="@+id/txtHorizontalCenter"
        android:text="取消" />

</RelativeLayout>

Java代码:
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private Button btnOpenDialog;
	private Activity activity;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		activity = this;
		btnOpenDialog = (Button) findViewById(R.id.btnOpenDialog);
		btnOpenDialog.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final Dialog dialog = new Dialog(activity);
				dialog.setContentView(R.layout.dialog_must_enter);
				dialog.setTitle("必填项Demo");

				final EditText edtUserName = (EditText) dialog.findViewById(R.id.edtUsername);
				Button btnOKOfDialog = (Button) dialog.findViewById(R.id.btnOK);
				btnOKOfDialog.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (!TextUtils.isEmpty(edtUserName.getText().toString().trim())) {
							dialog.dismiss();
						} else {
							Toast.makeText(activity, "姓名不能为空!", Toast.LENGTH_LONG).show();
						}

					}
				});

				Button btnCancelOfDialog = (Button) dialog.findViewById(R.id.btnCancel);
				btnCancelOfDialog.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
					}
				});

				dialog.show();

			}
		});
	}
}
 

 

 

你可能感兴趣的:(对话框)