···
public void onClick(View view) {
switch (view.getId()){
case R.id.bt_1://搜索
View inflate = View.inflate(this, R.layout.sousuo_layout, null);
final EditText mViewById = inflate.findViewById(R.id.sousuo1);
new AlertDialog.Builder(MainActivity.this)
.setTitle("请输入网址")
.setIcon(R.mipmap.ic_launcher_round)
.setView(inflate)
.setPositiveButton("确定", new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String url = mViewById.getText().toString().trim();
//判断输入的内容
if (url!=null&& !url.equals("")){
boolean infotext=infotext(url);
if (infotext){//数字和汉字
Toast.makeText(MainActivity.this,"请输入网址",Toast.LENGTH_SHORT).show();
}else{
//是字母然后判断字母拼接
if (url.indexOf("http://")==0 && url.indexOf(".com")==0){
//indexOf==0代表输入的能容有这个字符串
webView.loadUrl(url);
}else{
//判断是否有.com这个字符串
if (url.endsWith(".com")){
webView.loadUrl("http://"+url);
}else {
webView.loadUrl("http://"+url+".com");
}
}
}
}
}
}).setNegativeButton("取消",null).show();
break;
···
方法
···
public boolean infotext(String txt){
Pattern compile = Pattern.compile("[0-9]*");
Matcher matcher = compile.matcher(txt);//matcher匹配器
if (matcher.matches()){//matches相匹配的
return true;
}
Pattern compile1 = Pattern.compile("[a-zA-Z]");//正则表达式
matcher=compile.matcher(txt);
if (matcher.matches()){
return false;
}
char[] c=txt.toCharArray();//把传过来的String转换成char字符数组
for (int i = 0; i < c.length; i++) {//通过循环判断获取的char的每一个元素
Pattern compile2 = Pattern.compile("[\u4e00-\u9fa5]");
matcher=compile2.matcher(c[i]+"");//每一个元素判断是否是汉字
if (matcher.matches()){
return true;
}
}
return false;
}
···
网友评论