1

我在 aTextView中有一些以“#”开头的哈希标签

示例:“#one#two Hello World #three”。

我希望这些哈希标签可单独点击并打开一个活动并在该活动中获取此文本。

所以这些哈希作为一个链接工作并打开一个活动。标签也不是固定的,可能是任何文本。还将哈希标签的颜色更改为红色,其余标签的颜色将为黑色

示例: #one #two Hello World #three

4

2 回答 2

7

根据您的要求修改以下内容。用一个SpannableString

String s ="#one #Two Hello World #three";
String split[] = s.split("#");
TextView_tv = (TextView) findViewById( R.id.tv );
for(int i=1;i<split.length;i++)
{
  SpannableString ss1=  new SpannableString("#"+split[i]);     
  ss1.setSpan(new  MyClickableSpan(""+i), 0, 1,  Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  ss1.setSpan(newForegroundColorSpan(Color.RED),0,1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  _tv.append(ss1);
   _tv.append(" ");   
}
_tv.setMovementMethod(LinkMovementMethod.getInstance());

class MyClickableSpan extends ClickableSpan{    
String clicked;
public MyClickableSpan(String string) {

    super();
    clicked =string;
    }

    public void onClick(View tv) {
        if(clicked.equals("1"))
        {
             Toast.makeText(getApplicationContext(), "One",1000).show();
        }
        else if(clicked.equals("2"))
        {
            Toast.makeText(getApplicationContext(), "Two",1000).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(), "Three",1000).show();       
        }

   }

    @Override
    public void updateDrawState(TextPaint ds) {
       ds.setUnderlineText(false); // set to false to remove underline
    }
    } 
   }

捕捉模拟器

在每个哈希单击上显示吐司一、二和三。而不是 toast 开始一个新的活动。

在此处输入图像描述

编辑:

如果你想点击字符串

 ss1.setSpan(new  MyClickableSpan(""+i,split[i]), 0, 1,  Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

然后

 String clicked;
 String astring;
 public MyClickableSpan(String check,String actualstring) {
super();
clicked =check;
astring =actualstring; // pass this to next activity using intent
}

然后

  public void onClick(View tv) {
        if(clicked.equals("1"))
        {
             Toast.makeText(getApplicationContext(), astring,1000).show();
        }
        else if(clicked.equals("2"))
        {
            Toast.makeText(getApplicationContext(), astring,1000).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(), astring,1000).show();       
        }

   }
于 2013-11-11T15:03:28.143 回答
0

您可以使用LinearLayout

  • 在 xml 中添加:

    <EditText
        android:id="@+id/tag_ET"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="4"
        android:gravity="left"
        android:inputType="text" />
    
    <Button
        android:id="@+id/add_Btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="1"
        android:text="@string/tags_add_btn_text" />
    

    <LinearLayout
        android:id="@+id/tags_view"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:orientation="horizontal" />
    

  • 初始化布局:

    // EditText to write Tags.
    EditText tag = (EditText) findViewById(R.id.tag_ET));
    // Button used to add tags
    Button addTagBtn = (Button) findViewById(R.id.add_Btn));
    // layout contains added tags.
    final LinearLayout tagsView = (LinearLayout) findViewById(R.id.tags_view);
    
  • 将标签添加到 tagView :

        final String tagTxt = tagEt.getText().toString();
        if (!tagTxt.isEmpty()) {
            Spannable buttonLabel = new SpannableString(tagTxt);
    
            // clear tag edit text
            tagEt.setText("");
    
            // add tag to layout
            Button btn = new Button(this);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            lout.addView(btn, tagsView);
    
            btn.setText(buttonLabel);
            btn.setBackgroundResource(R.drawable.hover);
            btn.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // Toast whatever you want.
                    Toast.makeText(getApplicationContext(), tagTxt,1000).show();
                }
            });
        }
    
于 2014-08-18T12:57:41.397 回答