源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android 自动判断是电话,网址,EMAIL方法之Linkify的使用

  • 时间:2022-07-10 16:43 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android 自动判断是电话,网址,EMAIL方法之Linkify的使用
当我们在一个[b]EditText[/b]输入电话或者网址还是[b]Email[/b]的时候,让Android自动判断,当我们输入的是电话,我们点击输入内容将调用打电话程序,当我们输入是网址点击将打开浏览器程序.而[b]Linkify[/b]很好的解决了这个问题 步骤: [b]1、布局UI [/b]
[u]复制代码[/u] 代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
[b]2、在MainActivity中实现 [/b]
[u]复制代码[/u] 代码如下:
public class MainActivity extends Activity {  private TextView tv;  private EditText et;  @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);   tv = (TextView) findViewById(R.id.tv1);   et = (EditText) findViewById(R.id.et);   et.setOnKeyListener(new OnKeyListener() {    @Override    public boolean onKey(View v, int keyCode, KeyEvent event) {     tv.setText(et.getText());     // 判断输入的是URL还是EMAIL还是PHONENUMBER,并自动与系统连接     Linkify.addLinks(tv, Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS |);     return false;    }   });  } }
[b]OK!简便方法:在TextView中如下申明! [/b] [b]<TextView [/b] android:id="@+id/tv1"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  [b]android:autoLink="web|phone|email" />[/b]
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部