7

我已经挣扎了一段时间了。在需要文本的活动上:单击注册,即表示我同意服务条款和隐私政策。“服务条款”和“隐私政策”部分需要可点击。我找到的解决方案制作了网址。但我不需要 url 来浏览条款,不需要启动条款活动或隐私活动。

然后我发现了这个: https ://stackoverflow.com/a/9076448/1387161 但这里的问题是,它们没有对齐漂亮。我的意思是:如果有一个小分辨率的手机,我需要在textview2旁边设置textview1,在textview1下设置textview3,旁边是textview4。但是当我在平板电脑或大屏幕手机上使用该应用程序时,也许所有文本视图都可以彼此相邻,但布局与小屏幕手机上的布局保持一致=

文本视图 1 - 文本视图 2 文本视图 3 - 文本视图 4

一种可能的解决方案是 flowlayout,但我遇到了错误,似乎找不到一个好的教程(对于初学者) 如何使用 flowlayout(或任何自定义布局)

任何想法表示赞赏!

谢谢,

比约恩

4

4 回答 4

23
public class MainActivity extends Activity {
TextView _tv;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    _tv = (TextView) findViewById( R.id.textView1 );
    String sentence = "this is [part 1 clickable] and [part 2 clickable] and [part 3 clickable]";


   _tv.setMovementMethod(LinkMovementMethod.getInstance());
   _tv.setText(addClickablePart(sentence), BufferType.SPANNABLE);
}
private SpannableStringBuilder addClickablePart(String str) {
    SpannableStringBuilder ssb = new SpannableStringBuilder(str);

    int idx1 = str.indexOf("[");
    int idx2 = 0;
    while (idx1 != -1) {
        idx2 = str.indexOf("]", idx1) + 1;

        final String clickString = str.substring(idx1, idx2);
        ssb.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, clickString,
                        Toast.LENGTH_SHORT).show();
            }
        }, idx1, idx2, 0);
        idx1 = str.indexOf("[", idx2);
    }

    return ssb;
 }
}

编辑

public class MainActivity extends Activity {
TextView _tv;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    _tv = (TextView) findViewById( R.id.textView1 );

    SpannableString ss = new SpannableString("Android is a Software stack");

    ss.setSpan(new MyClickableSpan(), 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//22 to 27 stack is clickable
    ss.setSpan(new MyClickableSpan(), 0, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//0 to 7 Android is clickable

   _tv.setText(ss);
   _tv.setMovementMethod(LinkMovementMethod.getInstance());
}
class MyClickableSpan extends ClickableSpan{ //clickable span
    public void onClick(View textView) {
    //do something
       Toast.makeText(MainActivity.this, "Clicked",
            Toast.LENGTH_SHORT).show();
   }
    @Override
    public void updateDrawState(TextPaint ds) {
       ds.setColor(Color.GREEN);//set text color 
       ds.setUnderlineText(false); // set to false to remove underline
    }
}
}

有关 ClickableSpan 的更多信息http://developer.android.com/reference/android/text/style/ClickableSpan.html

您还可以通过设置粗体、斜体或设置字体大小来设置可扩展字符串的样式。

    StyleSpan boldSpan = new StyleSpan( Typeface.ITALIC );
    ss.setSpan( boldSpan, 22, 27, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
    StyleSpan boldSpan1 = new StyleSpan(Typeface.BOLD);
    ss.setSpan(new RelativeSizeSpan(3f), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//set fontsize
    ss.setSpan( boldSpan1, 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
于 2013-03-18T12:07:09.597 回答
1

如果您需要将 Raghunandan Answer 与 HTML 消息一起使用,请在您的显示功能中执行以下操作

public void displayText(String message) {

            chapterTextView.setText(Html.fromHtml(message),TextView.BufferType.SPANNABLE);
            chapterTextView.setMovementMethod(LinkMovementMethod.getInstance());
            Spannable clickableMessage = (Spannable) chapterTextView.getText();
            chapterTextView.setText(addClickablePart(clickableMessage), BufferType.SPANNABLE);
}

addClickablePart的修改功能

private SpannableStringBuilder  addClickablePart(Spannable charSequence) {
        SpannableStringBuilder  ssb = new SpannableStringBuilder(charSequence);

        int idx1 = charSequence.toString().indexOf("(");
        int idx2 = 0;
        while (idx1 != -1) {
            idx2 = charSequence.toString().indexOf(")", idx1) + 1;

            final String clickString = charSequence.toString().substring(idx1, idx2);
            ssb.setSpan(new ClickableSpan() {

                @Override
                public void onClick(View widget) {
                    Toast.makeText(getActivity(), clickString,
                            Toast.LENGTH_SHORT).show();
                }
            }, idx1, idx2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            idx1 = charSequence.toString().indexOf("(", idx2);
        }

        return ssb;
    }

希望这对某人有所帮助。

于 2014-08-04T23:15:20.823 回答
1
ClickableSpan cs = new ClickableSpan() {  
     @Override 
     public void onClick(View view) {  
        //action stuff here
     }  
};

String text_terms_string = getResources().getString(R.string.register_terms_text);
SpannableString ss = new SpannableString(text_terms_string);

int termStart = text_terms_string.indexOf("Terms");
int termStop = termStart + "term".length();

//we set here clickable our text  
ss.setSpan(cs, termStart, termStop, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//we set here our color i.e. #cccccc in this example I take color from xml  
ss.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.aquaBlue)),termStart,termStop,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//we set here text to be bolded    
ss.setSpan(new StyleSpan(Typeface.BOLD),termStart,termStop,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//I set here spannable text to my previous declared TextView  
text_terms_link.setText(ss);

//We set here clickable text, this is important !!  
text_terms_link.setMovementMethod(LinkMovementMethod.getInstance());
于 2015-05-21T11:20:20.253 回答
-2

好的,所以您需要使文本视图可点击,这很简单。创建文本视图对象并调用活动

      termnservc =(TextView)findViewById(R.id.srvid);
      termnservc.setOnClickListener(this);
       // use on click method
                    @Override
                public void onClick(View v) {                                         
            switch (v.getId()) {
            case R.id.srvid:
        Intent termsandcondition=new Intent(this, TermsOfService.class);
        startActivity(termsandcondition);
        break;
                    }

}

于 2013-03-18T11:53:32.380 回答