简体   繁体   中英

click link in textview

I have text view and I want to set-text as link to direct the site but the problem is that I cannot click:

                    Grades = (TextView) findViewById(R.id.textView9);
                    Grades.setText(Html.fromHtml(course.getString(TAG_Grade)));

                Grades.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {

                        WebView webView; webView.setWebViewClient(new WebViewClient()
                        {

                        }webView.loadUrl(course.getString(TAG_Grade)); });

And the xml:

<TextView 
    android:id="@+id/textView9"
    android:layout_marginLeft="110dp"
    android:layout_marginTop="365dp"
    android:textColor="#000"
    android:textSize="14sp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:autoLink="web"
    android:onClick="onClick"
/>

Knowing that course.getString(TAG_Grade) will get the url from db but it does not work

What is the problem?

Used this android:clickable="true"

<TextView 
    android:id="@+id/textView9"
    android:layout_marginLeft="110dp"
    android:layout_marginTop="365dp"
    android:textColor="#000"
    android:textSize="14sp"
    android:clickable="true"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     android:autoLink="web"
     android:onClick="onClick"
/>

You are trying to Linkify it after the view is clicked, removed the Linkify from within the onClick.


Your going about it the wrong way.

Try this:

String userCanSeeThis = "Your Website Name";
String url = course.getString(TAG_Grade);

TextView grades = (TextView) findViewById(R.id.textView9);
grades.setText(userCanSeeThis);

addLinks(Grades, userCanSeeThis, url);

Using this helper method:

    /**
     * @param textView
     *            textView who's text you want to change
     * @param linkThis
     *            a regex of what text to turn into a link
     * @param toThis
     *            the url you want to send them to
     */
    public static void addLinks(TextView textView, String linkThis, String toThis) {
        Pattern pattern = Pattern.compile(linkThis);
        String scheme = toThis;
        android.text.util.Linkify.addLinks(textView, pattern, scheme, new MatchFilter() {
            @Override
            public boolean acceptMatch(CharSequence s, int start, int end) {
                return true;
            }
        }, new TransformFilter() {

            @Override
            public String transformUrl(Matcher match, String url) {
                return "";
            }
        });
    }

Also if you set the onClickListener in your code with grades.setOnClickListener then your don't need android:onClick="" in your XML

除了使用Linkify我更喜欢以下行:

 Html.fromHtml(course.getString(TAG_Grade));
TextView myWebSite = new TextView(this);
myWebSite .setText("http://www.google.com/");
Linkify.addLinks(myWebSite , Linkify.WEB_URLS);

You have received enough answers regarding Linkify, but there is one more subtile error crawling in your code:

You are mistaking the attribute his android:onClick with the method onClick of an View.onClickListener :

The attribute android:onClick works as followed:

Name of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v) method of your context (typically, your Activity).

As for the onClick -method provided by the View.onClickListener -interface:

view.setOnClickListener(...)

Register a callback to be invoked when this view is clicked. If this view is not clickable, it becomes clickable.

Which will allow you to override the function :

public abstract void onClick (View v)

which is called when a view has been clicked.

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