我希望用户在edittext中输入IP地址并验证其是否有效。这是我的代码。如何验证?我也提到了此链接Validation in EditText allow IP or web Url host,但无法实现。请帮助
private static final String IPADDRESS_PATTERN =
"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ip_addr.isEmpty() ||!ip_addr.matches(IPADDRESS_PATTERN)) {
builder.setTitle("Something went wrong!!");
displayAlert("You have not entered valid IP Address");
}
}
}