Android的 - #标签中的TextView [英] Android - Hashtag in TextView

查看:152
本文介绍了Android的 - #标签中的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实现内部的TextView#标签?我想要做的就是实现内部的TextView可链接#标签。然后,用户可以点击它(包括hashtag),并切换到另一个片段。这是我的布局和片段。

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.

布局

<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>

片段

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);
    }
}

请指教。谢谢你。

推荐答案

您可以使用这个简单的库
HashTagHelper

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);

这篇关于Android的 - #标签中的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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