android方块密码输入框,Android仿微信/支付宝的方块密码输入框

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

不过自己也写了个简单的类似的。不废话了。没图说个席八。

0818b9ca8b590ca3270a3433284dd417.png

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

布局文件

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#ffffff"

android:paddingBottom="20dp"

android:paddingTop="30dp" >

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" />

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" >

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" />

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" />

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" />

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" />

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" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/layout"

android:layout_centerHorizontal="true"

android:layout_marginTop="20dp" >

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="确定" />

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="取消" />

代码:

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。下班。走人

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值