需求:
在EditText中输入信息,TextView中显示并可自动生成链接到拨号,发邮件,打开网页;操作时点击TextView即可
效果:
代码:
public class MainActivity extends Activity {
private TextView mTextView;
private EditText mEditText;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView)findViewById(R.id.textView1);
mEditText = (EditText)findViewById(R.id.editText1);
mEditText.setOnKeyListener(new EditText.OnKeyListener()
{
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
mTextView.setText(mEditText.getText());
Linkify.addLinks(mTextView,
Linkify.WEB_URLS |Linkify.EMAIL_ADDRESSES|Linkify.PHONE_NUMBERS);
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
重点:
1.linkify的用法,如何建立链接;
2.也可以静态建立链接,在TextView的属性中加入autoLink=“web|phone|email”