[android]EditText inputType 特殊情况得只有数字和特殊符号

这里是我的方案,

我只是想要只数目和特定的特殊字符,像元在 EditText 字段的结尾。

前: 234.34 元

我不想在此输入后验证,但宁可用户只能输入十进制数和在结束了这个特殊的字符。

一些人请帮我来做这个。

解决方法 1:

您可以使用以下 (progromatically)

ed.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
ed
.setKeyListener(DigitsKeyListener.getInstance("0123456789.$"));

您可以定义您想要在范围内的所有字符,DigitsKeyListener.getInstance

或者如果不想让用户输入一个 $ 符号和您想要在用户完成编辑后手动输入......

@Override
protected OnFocusChangeListener getOnFocusChangeListener() {
return new OnFocusChangeListener() {
   
@Override
   
public void onFocusChange(View v, boolean hasFocus) {
       
if (hasFocus) {
           
//Member variable in the class which contains an EditText
           
CurrencyTextbox.this.hadFocus = true;
       
} else {
           
// Thes EditText has lost focus
           
Log.v(TAG, "Lost focus.");
           
if (CurrencyTextbox.this.hadFocus) {
               
// We previously had focus, now we lost it, format the user input!
               
// Get current value of the Textbox
               
String value = CurrencyTextbox.this.textbox.getText().toString();
               
// Formatting the user input
                value
= String.format(//Doing some formatting);
               
// Reset the had focus
               
CurrencyTextbox.this.hadFocus = false;
           
}
       
}
   
}
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值