【问题标题】:Can button labels be shown as passwords with asterisk in android按钮标签可以在android中显示为带星号的密码吗
【发布时间】:2014-01-28 08:29:50
【问题描述】:

我有一个要求,我的登录表单以按钮的形式输入密码。 这是我的布局 xml。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:background= "@android:color/black"
        tools:context=".LoginActivity" >



        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView2"
            android:layout_below="@+id/textView2"
            android:layout_marginTop="26dp"
            android:text="Password"
            android:textColor="@android:color/darker_gray"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <Button
            android:id="@+id/button1"
            android:layout_width="65sp"
            android:layout_height="65sp"
            android:layout_alignLeft="@+id/textView3"
            android:layout_below="@+id/textView3"
            android:layout_marginTop="36dp"
            android:text="0"
            android:textColor="#00ff10"
            android:textSize="45sp" 

            />

        <Button
            android:id="@+id/Button02"
            android:layout_width="65sp"
            android:layout_height="65sp"
            android:layout_alignBaseline="@+id/Button01"
            android:layout_alignBottom="@+id/Button01"
            android:layout_toRightOf="@+id/Button01"
            android:textColor="#00ff10"
             android:textSize="45sp"
            android:text="0" />

        <Button
            android:id="@+id/Button03"
            android:layout_width="65sp"
            android:layout_height="65sp"
            android:layout_alignBaseline="@+id/Button02"
            android:layout_alignBottom="@+id/Button02"
            android:layout_toRightOf="@+id/Button02"
            android:textColor="#00ff10"
             android:textSize="45sp"
            android:text="0" />

        <Button
            android:id="@+id/Button01"
            android:layout_width="65sp"
            android:layout_height="65sp"
            android:layout_alignBaseline="@+id/button1"
            android:layout_alignBottom="@+id/button1"
            android:layout_toRightOf="@+id/button1"
            android:textColor="#00ff10"
            android:textSize="45sp"       
            android:text="0" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/textView3"
            android:layout_below="@+id/button1"
            android:layout_marginTop="76dp"
            android:textColor="@android:color/darker_gray"
            android:text="Login" />

        <Button
            android:id="@+id/Button04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/button2"
            android:layout_alignBottom="@+id/button2"
            android:layout_alignLeft="@+id/Button02"
            android:layout_marginLeft="18dp"
            android:textColor="@android:color/darker_gray"
            android:text="Reset" />

    </RelativeLayout>

这是活动代码sn-p

  switch (v.getId()) {
   case R.id.button1:
     b3 = (Button)findViewById(R.id.button1);

    int i3 = Integer.parseInt(b3.getText().toString());
    if(i3<=8)
    i3 = i3+1;
    else
    i3 = 0; 
    b3.setText(String.valueOf(i3));

    break;

多次点击按钮会像计数器一样显示点击次数。 因此,如果密码是 1234,请单击第一个按钮一次,然后单击第二个按钮两次 ,在第三个按钮三次和第四次按钮上四次。 我已经实现了这个,但试图找出一种使用星号隐藏标签的方法* 在用户单击按钮并在 * 出现之前看到输入的值之后。 我想知道这是否可以通过这种方法来完成?

【问题讨论】:

  • 您可以在每个按钮的 OnClickListener 中更改按钮的文本。
  • 嗨,yahska,可以在点击时产生延迟,以便用户可以在一段时间内看到所选择的值吗?不知道如何使用 setText 更改文本,当我尝试时它一直显示 *。我用活动代码 sn-p 更新了我的问题。
  • 现在根据我对问题的理解,您可以改用文本框。当用户在 TextWatcher 中输入一个数字并检查 onTextChanged 时。如果他插入了一位数字,则禁用该文本,这样对您来说更方便。如果用户插入了错误的数字,那么您已经拥有重置功能。所以这不会有问题。
  • 如果你想在这之间引入一些时间,那么就使用线程

标签: android button passwords


【解决方案1】:

你可以做的是点击任何一个按钮将其他按钮的文本设置为*。

Activity中创建4个int变量和4个button变量。

int pass1,pass2,pass3,pass4;
Button button1,button2,button3,button4;

初始化onCreate中的所有按钮。

在 onClick 中执行:

if(v.getId()==R.id.button1){
    pass1=(pass1+1)%10;
    button1.setText(""+pass1);
}else{
    if(pass1!=0)
        button1.setText("*");
}

if(v.getId()==R.id.button2){
    pass2=(pass2+1)%10;
    button2.setText(""+pass2);
}else{
    if(pass2!=0)
    button2.setText("*");
}

所有按钮都一样

您的密码是:

String password=""+pass1+pass2+pass3+pass4;

【讨论】:

  • 嗨 Vipul,这个建议接近我的要求,谢谢!尽管我仍在尝试在 2 秒的间隔后将标签淡化为 *。这可行吗?
【解决方案2】:

尝试为 EditText 实现此代码

editText.setHint("Password");
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 

【讨论】:

  • 嗨约翰,我没有在我的代码中使用 ant editText。你在哪里建议我使用你提供的这个代码?请参考我添加的 UI 图片。
  • 嗨,我会尝试用编辑文本替换标签并隐藏键盘。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
  • 2011-07-07
  • 2018-02-09
  • 2021-08-14
相关资源
最近更新 更多