实现不选择选项则不可以关闭的对话框之方法三
本方法基于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(); } }); } }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28