Android仿微信或支付宝方块密码输入框

 更新时间:2018年06月05日 11:12:57   作者:yaya_soft  
这篇文章主要为大家详细介绍了Android仿微信或支付宝方块密码输入框,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在用到支付类或者验证类app时,都有一个简密的输入框。百度了下有个不错的帖子,点击打开链接

不过自己也写了个简单的类似的。

懒得运行,直接截layout.xml的效果图先。

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:background="#ffffff" 
  android:paddingBottom="20dp" 
  android:paddingTop="30dp" > 
 
  <TextView 
    android:id="@+id/txtTitle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:text="请输入验证码" 
    android:textStyle="bold" 
    android:textSize="22sp" /> 
 
<LinearLayout 
  android:id="@+id/layout" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_below="@+id/txtTitle" 
  android:layout_centerHorizontal="true" 
  android:layout_marginTop="10dp" 
  android:orientation="horizontal" > 
 
  <TextView 
    android:id="@+id/t1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/i1" 
    android:gravity="center" 
    android:inputType="number" 
    android:lines="1" 
    android:maxLines="1" /> 
 
  <TextView 
    android:id="@+id/t2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/i1" 
    android:gravity="center" 
    android:inputType="number" 
    android:lines="1" 
    android:maxLines="1" /> 
  <TextView 
    android:id="@+id/t3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/i1" 
    android:gravity="center" 
    android:inputType="number" 
    android:lines="1" 
    android:maxLines="1" /> 
 
  <TextView 
    android:id="@+id/t4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/i2" 
    android:gravity="center" 
    android:inputType="number" 
    android:lines="1" 
    android:maxLines="1" /> 
 
</LinearLayout> 
 
<EditText 
  android:id="@+id/editHide" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_alignBottom="@+id/layout" 
  android:layout_alignLeft="@+id/layout" 
  android:layout_alignRight="@+id/layout" 
  android:layout_alignTop="@+id/layout" 
  android:layout_weight="1" 
  android:background="#00000000" 
  android:cursorVisible="false" 
  android:ems="10" 
  android:inputType="number" 
  android:maxLength="4" 
  android:textColor="#00000000" /> 
 
<LinearLayout 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_below="@+id/layout" 
  android:layout_centerHorizontal="true" 
  android:layout_marginTop="20dp" > 
 
  <Button 
    android:id="@+id/verifycode_ok" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#fa8d70" 
    android:layout_marginRight="30dp" 
    android:textColor="#ffffff" 
    android:text="确定" /> 
 
  <Button 
    android:id="@+id/verifycode_cancel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="30dp" 
     android:background="@color/index_time_but" 
    android:textColor="#ffffff" 
    android:text="取消" /> 
 
</LinearLayout> 
 
</RelativeLayout> 

代码:

import android.app.Activity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.widget.EditText; 
import android.widget.TextView; 
 
public class MainActivity extends Activity { 
 
  TextView t1, t2, t3, t4, et; 
 
  String key = ""; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.verifycode); 
    t1 = (TextView) findViewById(R.id.t1); 
    t2 = (TextView) findViewById(R.id.t2); 
    t3 = (TextView) findViewById(R.id.t3); 
    t4 = (TextView) findViewById(R.id.t4); 
    et = (EditText) findViewById(R.id.editText1); 
    et.addTextChangedListener(tw); 
  } 
 
  void setKey() { 
    char[] arr = key.toCharArray(); 
    t1.setText(""); 
    t2.setText(""); 
    t3.setText(""); 
    t4.setText(""); 
    for (int i = 0; i < arr.length; i++) { 
      if (i == 0) { 
        t1.setText(String.valueOf(arr[0])); 
      } else if (i == 1) { 
        t2.setText(String.valueOf(arr[1])); 
      } else if (i == 2) { 
        t3.setText(String.valueOf(arr[2])); 
      } else if (i == 3) { 
        t4.setText(String.valueOf(arr[3])); 
      } 
    } 
  } 
 
  TextWatcher tw = new TextWatcher() { 
    @Override 
    public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 
 
    } 
 
    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 
 
    } 
 
    @Override 
    public void afterTextChanged(Editable s) { 
      key = s.toString(); 
      setKey(); 
    } 
  }; 
} 

好哒,就这样了,运行就有效果了。没有什么自定义,没有什么第三方。

当然。如果你要弹窗形式的话,也一样,加下面的就能弹窗了

final AlertDialog dialog = new AlertDialog.Builder(mContext).create(); 
   dialog.show(); 
   dialog.getWindow().setContentView(R.layout.verifycode);t1 = (TextView) dialog.findViewById(R.id.t1); 
   t2 = (TextView) dialog.findViewById(R.id.t2); 
   t3 = (TextView) dialog.findViewById(R.id.t3); 
   t4 = (TextView) dialog.findViewById(R.id.t4); 

后期有时间再补上项目DOME。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android登录界面的实现代码分享

    Android登录界面的实现代码分享

    好久没有搞android项目了,手都有点松了,今天因为项目的需要,继续弄android知识,在项目中登录界面是项目中比较常见的最基本的功能,对android登录界面的实现感兴趣的朋友一起学习吧
    2016-11-11
  • Android评论图片可移动顺序选择器(推荐)

    Android评论图片可移动顺序选择器(推荐)

    这篇文章主要介绍了 Android评论图片可移动顺序选择器的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-12-12
  • android自定义Toast设定显示时间

    android自定义Toast设定显示时间

    这篇文章主要为大家详细介绍了android自定义Toast设定显示时间,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-08-08
  • Android自定义view绘制圆环占比动画

    Android自定义view绘制圆环占比动画

    这篇文章主要为大家详细介绍了Android自定义view绘制圆环占比动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • Android5.0新特性详解之全新的动画

    Android5.0新特性详解之全新的动画

    这篇文章主要介绍了Android5.0新特性详解之全新的动画的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-09-09
  • Android Studio调试Gradle插件详情

    Android Studio调试Gradle插件详情

    这篇文章主要介绍了Android Studio调试Gradle插件详情,文章围绕主题展开详细的内容戒杀,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09
  • Android Kotlin Flow 冷热流详解

    Android Kotlin Flow 冷热流详解

    在Android开发中,Flow是Kotlin协程库的重要组成部分,用于处理异步数据流,它能够异步生产多个值,类似于RxJava中的Observable,本文介绍Android Kotlin Flow 冷热流,感兴趣的朋友一起看看吧
    2024-11-11
  • Android中注解处理器APT用法示例

    Android中注解处理器APT用法示例

    APT全称Annotation Processing Tool,即注解处理器,APT是一种处理注释的工具, 它对源代码文件进行检测找出其中的注解,并使用注解进行额外的处理,给我们自动生成代码,简化使用,很多流行框架都使用到了APT技术,如 ButterKnife,Retrofit,Arouter,EventBus 等等
    2023-12-12
  • Android SDK中的Support兼容包详解

    Android SDK中的Support兼容包详解

    这篇文章主要介绍了Android SDK中的Support兼容包详解,本文详细区分了Support Library的版本区别、各种Theme的概念和使用注意事项等内容,需要的朋友可以参考下
    2015-05-05
  • Android仿网易新闻图片详情下滑隐藏效果示例代码

    Android仿网易新闻图片详情下滑隐藏效果示例代码

    这篇文章主要给大家介绍了关于利用Android如何仿网易新闻图片详情下滑隐藏效果的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-07-07

最新评论