第一:要获取到输入的数字前必须获取到EditText中的所有数据,查看API,有如下方法
返回参数类型 方法名 方法的释义Editable getText() Return the text the TextView is displaying.通过该方法可以返回TextView正在显示的文字,而返回值的类型是Editable,这个类型看上去比较陌生
再看看Editable的API,如下:
public interfaceEditableimplements GetChars Spannable Appendable CharSequence
然后在CharSequence中有一个方法
abstract String toString() Returns a string with the same characters in the same order as in this sequence.
然后就可以获得一个String对象,然后对String的操作是不是方便多了,通过取出每一个字符判断是否为数字,代码如下:
String str=et_input.getText().toString();if(str != null && !"".equals(str)){ for(int i=0;i=48 && str.charAt(i)<=57){//匹配数字 str2+=str.charAt(i); }}System.out.println(str2);
取消
评论