java - Android - TextView 中的标签

标签 java android android-layout android-fragments

如何在TextView中实现hashtag?我想要做的是在 textview 中实现可链接的主题标签。然后用户可以单击它(主题标签)并切换到另一个 fragment 。这是我的布局和 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>

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

最佳答案

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

它的用法非常简单:

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

关于java - Android - TextView 中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30072793/

相关文章:

java - Codility 超车 - 如何解决这个问题

java - 在Windows启动时执行Jar不起作用,没有结果

java - 用于特定于语言环境的双格式的 String.format 选项,例如 Double.toString()?

android - 找不到应用程序 :layout_scrollflags from CollapsingToolbarLayout 的资源 ID

android - Android XML 布局中条件语句的语法

java - 如何将目录中的文件列表从 PHP 传递到 Android/Java

android - 卡在添加 View 中

java - 如何将一个组合框的数据影响到另一个在 Swing 中两个不同类中可用的组合框的数据?

android - ImageView 与 ImageButton 与按钮

java - 我应该在该函数中更改什么以获得类似 0.00 的效果?