Android Studio 如何实现PIN码屏幕

hsvhsicv  于 2023-01-05  发布在  Android
关注(0)|答案(5)|浏览(212)

如何编码这种类型的密码屏幕设计,而在输入密码后,圆圈应改变或图像视图应改变..??
登录名.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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/light_grey"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter MPIN"
        android:textStyle="bold"
        android:textSize="18dp"
        android:layout_margin="10dp"
        android:layout_gravity="center_horizontal"
        />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal"
        >
        <ImageView
            android:id="@+id/imageview_circle1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="6dp"
            android:src="@drawable/circle"
            />
        <ImageView
            android:id="@+id/imageview_circle2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="6dp"
            android:src="@drawable/circle"
            />
        <ImageView
            android:id="@+id/imageview_circle3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="6dp"
            android:src="@drawable/circle"
            />
        <ImageView
            android:id="@+id/imageview_circle4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="6dp"
            android:src="@drawable/circle"
            />
    </LinearLayout>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/light_grey2"
    >
    <EditText
        android:id="@+id/editText_enter_mpin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:maxLength="4"
        android:visibility="visible" >
    </EditText>
</LinearLayout>
</LinearLayout>
</LinearLayout>

登录活动.java

public class LoginActivity extends AppCompatActivity {

private static final String TAG = "LoginActivity";
EditText enter_mpin;
ImageView i1, i2, i3, i4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    i1 = (ImageView) findViewById(R.id.imageview_circle1);
    i2 = (ImageView) findViewById(R.id.imageview_circle2);
    i3 = (ImageView) findViewById(R.id.imageview_circle3);
    i4 = (ImageView) findViewById(R.id.imageview_circle4);

    enter_mpin = (EditText) findViewById(R.id.editText_enter_mpin);
    enter_mpin.requestFocus();
    enter_mpin.setInputType(InputType.TYPE_CLASS_NUMBER);
    enter_mpin.setFocusableInTouchMode(true);

    enter_mpin.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            Log.d(TAG, "onKey: screen key pressed");
            i1.setImageResource(R.drawable.circle2);
        }
    });
}
}

圆圈.xml

<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
    android:width="2dp"
    android:color="#6ab17b" />
<size
    android:width="25dp"
    android:height="25dp" />
</shape>

圆圈2.xml

<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
    android:color="#505253"/>
<size
    android:width="25dp"
    android:height="25dp" />
</shape>

pftdvrlh

pftdvrlh1#

使用LolliPin

    • 材质设计Android密码库。支持指纹。**

<com.github.orangegangsters.lollipin.lib.views.PinCodeRoundView
                android:id="@+id/pin_code_round_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/pin_code_round_top_margin"
                android:layout_marginBottom="@dimen/pin_code_elements_margin"
                app:lp_empty_pin_dot="@drawable/pin_empty_dot"
                app:lp_full_pin_dot="@drawable/pin_full_dot"/>
z9smfwbn

z9smfwbn2#

在TextWatcher中使用以下代码:

@Override
            public void afterTextChanged(Editable s) {
                switch (s.length()) {
                    case 4:
                        i4.setImageResource(R.drawable.circle2);
                        break;
                    case 3:
                        i4.setImageResource(R.drawable.circle);
                        i3.setImageResource(R.drawable.circle2);
                        break;
                    case 2:
                        i3.setImageResource(R.drawable.circle);
                        i2.setImageResource(R.drawable.circle2);
                        break;
                    case 1:
                        i2.setImageResource(R.drawable.circle);
                        i1.setImageResource(R.drawable.circle2);
                        break;
                    default:
                        i1.setImageResource(R.drawable.circle);
                }
            }
guykilcj

guykilcj3#

当我在我的项目中添加棒棒糖时,我得到了一些错误。
然后我发现了一些更多的图书馆,也很棒。
看看这些:

  1. PinLockView
  2. BlurLockView
    请记住,这些都是视图,所以它们更容易定制。快乐编码:)
pvcm50d1

pvcm50d14#

看看这个JAVA class。如果你需要很多PIN视图,你也可以创建多个对象,每个对象只有一行。例如在注册页面。

pbpqsu0x

pbpqsu0x5#

将**implementation 'com.alimuzaffar.lib:pinentryedittext:1.3.10'**添加到 build.gradle 文件内的依赖项中,并在登录布局中保留以下代码。

<com.alimuzaffar.lib.pin.PinEntryEditText
    android:id="+id/pass" 
    android:layout_width="250dp"    
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:maxLength="6"
    android:inputType="numberPassword" />


#xml #安卓系统

相关问题