SpannableString和ClickableSpan的问题

时间:2019-01-23 07:29:09

标签: java android uiscrollview spannablestring clickablespan

我写了这段代码:

public class BaldrActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.a_dei_baldr);

    String myString = "Click Here!";

    SpannableString ss = new SpannableString(myString);

    ClickableSpan clickableSpan1 = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            Toast.makeText(BaldrActivity.this, "hello world", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(Color.BLUE);
            ds.setUnderlineText(false);
        }
    };

    ss.setSpan(clickableSpan1, 6, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    TextView tv = findViewById(R.id.tv_baldr1);
    tv.setText(myString);
    tv.setMovementMethod(LinkMovementMethod.getInstance());
}

}

但是...什么都没有改变。我的意思是,代码正确构建,应用程序不会崩溃,TextView实际上在“ Click Here!”中更改,但是它不可单击并且外观也不更改...为什么?

1 个答案:

答案 0 :(得分:0)

好像您在一行中设置String:

Word.run(function (context) {

    // Queue a command to get the current selection and then
    // create a proxy range object with the results.
    var range = context.document.getSelection();

    // Queue a commmand to insert OOXML in to the beginning of the range.
    range.insertOoxml("PASTE ABOVE MERGE XML", Word.InsertLocation.start);

    // Synchronize the document state by executing the queued commands,
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        console.log('OOXML added to the beginning of the range.');
    });
})
.catch(function (error) {
    console.log('Error: ' +JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

相反,设置要创建的SpannableString:

tv.setText(myString);