选择器与spanable不工作在Android M但在M下面正常工作

时间:2016-01-12 07:11:08

标签: android layout spannablestring android-selector android-text-color

我正在使用此stackoverflow链接创建一个带有spannable string of textview的选择器 (Change the text color of a single ClickableSpan when pressed without affecting other ClickableSpans in the same TextView),

它在除android-M之外的所有Android版本上运行正常,我无法检测到原因,所以请帮助我们。

基本上我的观点就像这个,https://drive.google.com/file/d/0BwkVxZWl7VcEVkFTQVRNbE9sLTA/view?usp=sharing,我想在立即注册时选择一个选择器,但这不能影响文本的其余部分,而且一切都必须是单文本视图或spannable

import android.text.SpannableString;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import cl.dummy.R;
import cl.dummy.utility.spannablee.LinkTouchMovementMethod;
import cl.dummy.utility.spannablee.TouchableSpan;


public class Login extends AppCombatActivity 
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

                init();
    }

    private final void init()
    {
        final TextView myTextView =  GeneralFunctions.findViewByIdAndCast(this, R.id.login_tv_noAccount);
        final SpannableString mySpannable = new SpannableString(myTextView.getText().toString());
        final TouchableSpan touchableSpan = new TouchableSpan(ContextCompat.getColor(this, R.color.color60),ContextCompat.getColor(this, R.color.colorBlue),Color.TRANSPARENT) {
            @Override
            public void onClick(View textView) {
                GeneralFunctions.simpleMoveToNextActivity(Register.class, Login.this,null);
            }
        };

                myTextView.setMovementMethod(new LinkTouchMovementMethod());
                myTextView.setHighlightColor(ContextCompat.getColor(Login.this, android.R.color.transparent));

                mySpannable.setSpan(touchableSpan, GeneralFunctions.getText(myTextView).indexOf("Register"), GeneralFunctions.getText(myTextView).length(), 0);
                myTextView.setText(mySpannable, TextView.BufferType.SPANNABLE);
    }
}

1 个答案:

答案 0 :(得分:1)

我通过将textview转换为按钮解决了上述问题,但它运行良好,但我不知道为什么它在android M中没有使用textview的原因