有时候需要对EditText的输入进行监听,一旦用户输入不符合规则,立刻提示并阻止。只需要为其绑定一个监听器OnKeyListener。代码很简单,功能很强大:
mEditText.setOnKeyListener(new EditText.OnKeyListener()
{
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2)
{
mTextView.setText(mEditText.getText());
return false;
}
});
有时候在软件的关于信息中给出开发单位的客服电话,用户只需要点一下这个电话就能播出去或者跳转到拨号界面,代码如下:
Linkify.addLinks(mTextView, Linkify.WEB_URLS
| Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS);
自动拨号出去,省去跳转到拨号界面后再手动,如下:
public void callPhone(Context context, String phone) {
if (phone.trim().length() > 0) {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"
+ phone.replaceAll("\\-", "")));
context.startActivity(callIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
不过为了代码健壮些,最好用正则表达式先对phone做个判断再拨号
最后加个权限:
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CALL_PRIVILEGED" />