Dialog自定义框添加密码锁

要求是需要在一个apk进入的时候添加密码锁。

首先我想到是使用安卓自带的Dialog去完成这个功能,但是做到最后一步的时候发现了一个大问题,当我设置了setPositiveButton、setNegativeButton这两个按钮的时候我发现当我按下不管密码正确还是错误都能够进入到apk界面,所以这个方法行不通,只有通过Dialog自定义布局去完成这个功能。

先看看布局吧

<?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:id="@+id/rc_document_edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:imeOptions="flagNoEnterAction"
        android:inputType="textNoSuggestions"
        android:maxLines="5"
        android:textColor="#000000"
        android:textSize="@dimen/font_size_medium" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:background="?android:attr/dividerHorizontal" />

    <LinearLayout
        android:id="@+id/LinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:measureWithLargestChild="true"
        android:orientation="horizontal"
        android:padding="0dp" >

        <Button
            android:id="@+id/cancelBtn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?android:attr/selectableItemBackground"
            android:text="@string/cancel" />

        
        <View
            android:id="@+id/postCancelBtnDivider"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="?android:attr/dividerHorizontal" />
        
        <Button
            android:id="@+id/okBtn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="?android:attr/selectableItemBackground"
            android:text="@string/ok" />
    </LinearLayout>

</LinearLayout>
布局完成,效果图就不展示了,不方便截图。

首先我们通过Android生命周期知道我们需要在onResume这里面实现。

		//由于是在fragment所以必须使用getActivity();
		LayoutInflater inflater = getActivity().getLayoutInflater();
		//将一个xml定义的布局文件实例化为view控件对象
		final View view = inflater.inflate(R.layout.custom_message_rename, null);
		//最终会调用的方法
		final EditText rc_document_edit = (EditText)view.findViewById(R.id.rc_document_edit);
		//创建方法,使其使用操作
		Button cancelBtn = (Button)view.findViewById(R.id.cancelBtn);
		Button okBtn = (Button)view.findViewById(R.id.okBtn);
		
		
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setView(view);
		builder.setTitle("Please input password");
		 ad= builder.create();
		 cancelBtn.setOnClickListener(new View.OnClickListener(){
            @Override
			//按下退出按钮
            public void onClick(View v){
               finish();
            }
        });
        okBtn.setOnClickListener(new View.OnClickListener(){
            @Override
			//按下确认按钮
            public void onClick (View v){
				//获取输入字符串
               String str= rc_document_edit.getText().toString().trim();
					if(str.equals("1234"))//比较是否相同
					{
						ad.dismiss();
					}else{
						rc_document_edit.setText("");
						Toast.makeText(getActivity(),"password error",Toast.LENGTH_SHORT).show();
					}
            }
        });
		ad.show();

还有定义ad,在onResume函数外添加AlertDialog ad;这样就成功的完成设置密码功能。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值