在Android的\\ Eclipse的可点击的TextView [英] Clickable TextView in Android\Eclipse

查看:364
本文介绍了在Android的\\ Eclipse的可点击的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android中创建可点击textviews没有成功,在布局的android我已经正确的输出,但没有可链接。

这就是我现在所拥有的:

 <的TextView
    机器人:ID =@ + ID / textTitle
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:自动链接=网
    机器人:可点击=真
    机器人:TEXTSIZE =16SP
    机器人:文字样式=大胆/>
        textTitle.setText(Html.fromHtml(response.toString()));
        textTitle.setMovementMethod(LinkMovementMethod.getInstance());

  Log.i(myApp1,response.toString());

我有

 < A HREF = HTTP://...> myLink的< / A>


解决方案

您只需应该注册一个监听器,这个的TextView

  findViewbyId(R.id.textTitle).setOnClickListener(新OnClickListener {
    @覆盖
    保护无效的onClick(查看视图){
        字符串link =(TextView中)view.getText()的toString()。
        / *重定向到这里网址,例如:* /
        意向意图=新意图(Intent.ACTION_VIEW,Uri.parse(链接));
        startActivity(意向);
    }
});

或者

添加的android:自动链接=所有属性为你的的TextView 并设置其文本的HTML A 元素。例如:

 <的TextView
    机器人:文字=@字符串/ myLink的
    机器人:自动链接=所有/>

而在的strings.xml

 &LT;字符串名称=myLink的&GT;&LT; A HREF =HTTP://www.google.com'> HTTP://www.google.com< / A&GT;&LT; /串&GT;

I'm trying to create clickable textviews in Android without success, in layout android I've the correct output but not linkable.

Here's what I have right now:

<TextView
    android:id="@+id/textTitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:clickable="true"
    android:textSize="16sp"
    android:textStyle="bold" />  


        textTitle.setText(Html.fromHtml(response.toString()));
        textTitle.setMovementMethod(LinkMovementMethod.getInstance());

In

Log.i("myApp1", response.toString());

I've

<a href=http://...>MyLink</a>

解决方案

You just should register a listener to this TextView

findViewbyId(R.id.textTitle).setOnClickListener(new OnClickListener{
    @Override
    protected void onClick(View view){
        String link = (TextView)view.getText().toString();
        /* redirect to URL here for example: */
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
        startActivity(intent);
    }
});

Or:

Add android:autoLink="all" property to your TextView and set its text a HTML A element. for example:

<TextView
    android:text="@string/mylink"
    android:autoLink="all"/>

And in strings.xml:

<string name="mylink"><a href='http://www.google.com'>http://www.google.com</a></string>

这篇关于在Android的\\ Eclipse的可点击的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆