网站开发语言java和php/宁波seo网络推广外包报价
我应该使用什么android:inputType输入IP地址?
我正在构建一个小型Android应用程序,用户将在其中将IP地址或主机名输入到EditText小部件中。 他们有90%的时间将输入IP地址,其余时间是主机名。
因此,自然而然地,我想进行优化以使其易于输入IP地址,但是切换到字母数字以输入主机名的能力很重要。
我似乎找不到一个好的inputType。 2746238348753853896448最初看起来像是一个好镜头,但只允许一个点。
理想情况下,我想从按下2746238348753896496448按钮的标准键盘开始。
我如何到达那里?
11个解决方案
55 votes
尝试使用android:inputType="number",但还要设置android:digits="0123456789."。适合我。
Bodacious answered 2020-02-13T20:38:20Z
40 votes
如果您使用inputType="phone",则可以访问包含数字和句点字符的简化键盘-这不会限制您可以输入的句点数量。
输入时请查看此答案以进行验证。
Graeme answered 2020-02-13T20:38:45Z
21 votes
通过添加android:inputType =“ number | numberDecimal”和android:digits =“ 0123456789”,可以完美地使用数字和十进制键盘。
例
android:id="@+id/ip_address"
android:inputType="number|numberDecimal"
android:digits="0123456789."
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Munish Kapoor answered 2020-02-13T20:39:09Z
13 votes
您可以使用自己的输入过滤器
final EditText text = new EditText(ServerSettings.this);
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start,
int end, Spanned dest, int dstart, int dend) {
if (end > start) {
String destTxt = dest.toString();
String resultingTxt = destTxt.substring(0, dstart) +
source.subSequence(start, end) +
destTxt.substring(dend);
if (!resultingTxt.matches ("^\\d{1,3}(\\." +
"(\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3})?)?)?)?)?)?")) {
return "";
} else {
String[] splits = resultingTxt.split("\\.");
for (int i=0; i
if (Integer.valueOf(splits[i]) > 255) {
return "";
}
}
}
}
return null;
}
};
text.setFilters(filters);
SKT answered 2020-02-13T20:39:29Z
6 votes
用这个 :
android:id="@+id/txtIP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="0123456789."
/>
Richi answered 2020-02-13T20:39:49Z
2 votes
android:id="@+id/ip_address"
android:inputType="number|numberDecimal"
android:digits="0123456789."
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
为我工作
saurabh yadav answered 2020-02-13T20:40:09Z
1 votes
我认为您唯一的选择是
EditText android:inputType="text" ... />
您可以检查IP地址包含的3点文本
coder_For_Life22 answered 2020-02-13T20:40:33Z
1 votes
我认为您需要使用TextWatcher进行验证,并使用TextView.addTextChangedListener()方法进行注册,并使用Pattern.DOMAIN_NAME和Pattern.IP_ADDRESS(Android 2.2+)。
看到:
Android:如何验证EditText输入?
在Android中验证IP
user802421 answered 2020-02-13T20:41:06Z
1 votes
您可以扩展DigitsKeyListener(源)并更改filter()函数(将检查ip模式或字符串主机名的验证)和getInputType()为return InputType.TYPE_CLASS_PHONE;
Jana answered 2020-02-13T20:41:26Z
0 votes
也许如果您使用2个单选按钮,一个显示主机的编辑文本,另一个显示IP的4个数字编辑文本,那么,一旦用户提交数据,您就将所有4个编辑文本值与它们之间的点连接起来,就像这样,edittext1.getText( )+“。 + edittext2.getText()+“。” edittext3.getText()+“。” edittext4.getText(),因此您可以像这样获得一个经过验证的IP地址,但是显然它将意味着更多的工作。
crit_ answered 2020-02-13T20:41:47Z
0 votes
尝试使用android:inputType="textUri"。当您需要主机名或IP地址时,它特别好用。
Sabaat Ahmad answered 2020-02-13T20:42:07Z