标签:
代码:
/** * 弹出设置防盗密码对话框 */ private void showSetupPwdDialog() { //弹出设置的对话框 Builder builder = new Builder(this); //给当前对话框填充一个布局文件 //TODO 此处的this代表的是?HomeActivity View view = View.inflate(this, R.layout.dialog_setup_password, null); //获取输入的密码和确认密码 final EditText et_password = (EditText) view.findViewById(R.id.et_password); final EditText et_password_confirm = (EditText) view.findViewById(R.id.et_password_confirm); //给对话框的确认和取消两个按钮设置监听,注意button是定义在dialog_setup_password.xml里的,而此处的this代表HomeActivity Button bt_ok = (Button) view.findViewById(R.id.ok);//在弹出的对话框里找button Button bt_cancel = (Button) view.findViewById(R.id.cancel); bt_ok.setOnClickListener(new OnClickListener() { //当点击确认时触发此方法,即设置防盗密码 @Override public void onClick(View v) { //对输入的密码进行判断,并用sp保存 //得到密码 String password = et_password.getText().toString().trim(); String password_confirm = et_password_confirm.getText().toString().trim(); //判空,判是否相等 if (TextUtils.isEmpty(password)) { Toast.makeText(HomeActivity.this, "密码不能为空!", 1).show(); }else if (TextUtils.isEmpty(password_confirm)) { Toast.makeText(HomeActivity.this, "确认密码不能为空!", 1).show(); }else if (!password_confirm.equals(password_confirm)) { Toast.makeText(HomeActivity.this, "两次密码不一致!", 1).show(); }else{ //保存密码 Editor editor = sp.edit(); editor.putString("password", MD5Utils.encode(password)); editor.commit(); Toast.makeText(HomeActivity.this, "设置成功!", 1).show(); //进入防盗设置的主页面 Intent intent = new Intent(HomeActivity.this,LostFindActivity.class); startActivity(intent); dialog.dismiss(); } } }); bt_cancel.setOnClickListener(new OnClickListener() { //当点击取消时触发此方法,销毁提示框 @Override public void onClick(View v) { dialog.dismiss(); } }); // builder.setView(view); // dialog = builder.show(); //为解决在低版本运行由dialog引发的边框为黑色的问题 dialog = builder.create(); //将四周的spacing都设置为0 // dialog.setView(view, viewSpacingLeft, viewSpacingTop, viewSpacingRight, viewSpacingBottom) dialog.setView(view, 0, 0, 0, 0); dialog.show(); }
布局:dialog_setup_password.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="300dip" android:background="#ffffff" //低版本2.3适配
android:gravity="center_horizontal" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:textColor="#000000" android:textSize="18sp" android:layout_width="300dip" android:layout_height="50dip" android:background="#66ff6600" android:gravity="center" android:text="设置密码" /> <EditText android:id="@+id/et_password" android:layout_width="280dip" android:layout_height="wrap_content" android:hint="请输入您要设置的密码" android:inputType="textPassword" > </EditText> <EditText android:id="@+id/et_password_confirm" android:layout_width="280dip" android:layout_height="wrap_content" android:hint="请再次输入您要设置的密码" android:inputType="textPassword" /> <LinearLayout android:gravity="center_horizontal" android:layout_width="300dip" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/ok" android:layout_width="140dip" android:layout_height="wrap_content" android:text="确定" /> <Button android:id="@+id/cancel" android:layout_width="140dip" android:layout_height="wrap_content" android:text="取消" /> </LinearLayout> </LinearLayout>
标签:
原文地址:http://www.cnblogs.com/wanghongwei/p/4312237.html