简体   繁体   中英

Clickable TextView in Android\Eclipse

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>

XML视图是绝对正确的,请确保您已正确实用地处理了onClickListener。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM