Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

java - Android - Hashtag in TextView

How to implement hashtag inside TextView? What I want to do is implement linkable hashtag inside textview. Then user can click on it (hashtag) and switch to another fragment. This is my layout and fragment.

Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainFragment" >

    <TextView
            android:id="@+id/txtHashtag"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Iena I #love you !!!"/>
</RelativeLayout>

Fragment

package com.xxxx;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xxxxx.R;

public class MainFragment extends Fragment{

    private TextView txtHashtag;
    public MainFragment() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.activity_main_fragment,
                null);

        txtHashtag = (TextView) v.findViewById(R.id.txtHashtag);

        return v;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use this simple library HashTagHelper

It has very simple usage:

mHashTagText = (TextView) findViewById(R.id.text);
mTextHashTagHelper = HashTagHelper.Creator.create(getResources().getColor(R.color.colorPrimary), 
    new HashTagHelper.OnHashTagClickListener() {
        @Override
        public void onHashTagClicked(String hashTag) {

        }
});

// pass a TextView or any descendant of it (incliding EditText) here.
// Hash tags that are in the text will be hightlighed with a color passed to HasTagHelper

mTextHashTagHelper.handle(mHashTagText);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...