public static boolean isEmail(String strEmail) { String strPattern = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$"; Pattern p = Pattern.compile(strPattern); Matcher m = p.matcher(strEmail); return m.matches(); }
检查EditText中输入的是否符合规则:
import Android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class Main extends Activity { private EditText editText; private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText = (EditText) findViewById(R.id.textId); editText.setText("EditText element"); button = (Button) findViewById(R.id.btnId); button.setText("Check"); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (checkString(editText.getText().toString())) { editText.setText("Corect"); } } }); } private boolean checkString(String s) { return s.matches("\\w*[.](Java|cpp|class)"); } }