android 4.4 HorzontalScrollView嵌套EditText,singleLine = true或inputtype=number引起滚动的解决办法

来源:互联网 发布:分形理论 知乎 编辑:程序博客网 时间:2024/04/29 15:44

在使用到HorzontalScrollView进行页面切换的时候,某个页面有个EditText,此输入框只能输入数字,不管是在xml设置了android:singleLine="true"或android:inputType="number"还是在代码里面动态设置 setSingleLine(true)或 setInputType(EditorInfo.TYPE_CLASS_NUMBER),

当准备输入出现软件盘的时候,HorzontalScrollView滑动到了下一个页面,软键盘消失的时候HorzontalScrollView继续滑动到下下一个页面。

出现这样的问题,原因暂时没找到,目前只有解决办法,在保证输入框只能输入正整数的前提下有三个解决办法:


(1) 为EditText设置一个DigitsKeyListener  

 public DigitsKeyListener(boolean sign, boolean decimal) 参数sign 表示是否有符号, 参数decimal 表示是否允许小数
 

 Eg:


 etRuntime.setKeyListener(new DigitsKeyListener(false,false)); //只允许输入正整数


(2) 为EditText中设置属性,android:numeric="integer"即只能输入正整数


(3) 为EditText设置一个NumberKeyListener,然后重写NumberKeyListener的getAcceptedChars()方法和KeyListener的getInputType()方法  


Eg: 只允许输入正整数
holder.etRuntime.setKeyListener(new NumberKeyListener()
{
@Override
public int getInputType()
{
// TODO Auto-generated method stub
return android.text.InputType.TYPE_CLASS_NUMBER; 
}

@Override
protected char[] getAcceptedChars()
{
// TODO Auto-generated method stub
return new char[] { '1', '2', '3', '4', '5', '6', '7', '8','9', '0' };
}
}); 




0 0
原创粉丝点击