使textview的部分可点击(不是网址)

时间:2013-03-18 11:38:38

标签: android android-layout

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

然后我发现了这个: https://stackoverflow.com/a/9076448/1387161 但问题在于,它们并不是美丽的。我的意思是:如果有一个分辨率很小的手机,我需要在textview1旁边的textview2和textview3旁边设置textview1,旁边是textview4。但是当我在平板电脑或具有更大屏幕的手机上使用该应用程序时,也许所有的文本视图可以站在彼此旁边,但是布局保持与使用小屏幕的电话相同=

Textview1 - Textview2 Textview3 - Textview4

一个可能的解决方案可以是flowlayout,但我收到错误,似乎找不到一个好的教程(适合初学者) How to use flowlayout (or any custom layout)

感谢任何想法!

THX,

比约

4 个答案:

答案 0 :(得分:22)

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

的更多信息

您还可以通过使用粗体,斜体或设置字体大小来设置spannable字符串的样式。

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

答案 1 :(得分:1)

如果您需要使用带有HTML消息的Raghunandan答案,请执行以下操作 在您的显示功能

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

希望这有助于某人。

答案 2 :(得分:0)

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

答案 3 :(得分:-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;
                    }

}

相关问题