android中TextView 添加ClickableSpan後點擊選中文字背景問題

來源:互聯網
上載者:User

標籤:

TextView中的setHighlightColor(int color)用於設定選中文字背景色高亮顯示。

 比如以下:

public class MainActivity extends ActionBarActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        FrameLayout frameLayout = new FrameLayout(this);        setContentView(frameLayout);        frameLayout.setId(R.id.container);        if (savedInstanceState == null) {            getSupportFragmentManager().beginTransaction()                    .add(R.id.container, new PlaceholderFragment())                    .commit();        }    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }    /**     * A placeholder fragment containing a simple view.     */    public static class PlaceholderFragment extends Fragment {        public PlaceholderFragment() {        }        @Override        public View onCreateView(LayoutInflater inflater, ViewGroup container,                                 Bundle savedInstanceState) {            View rootView = inflater.inflate(R.layout.fragment_main, container, false);            return rootView;        }        @Override        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {            super.onViewCreated(view, savedInstanceState);            TextView textView = (TextView) view.findViewById(R.id.textview);            String str = "Click me!";            String txt = str + "Hello world!";            SpannableString spannableString = new SpannableString(txt);            ClickableSpan clickableSpan = new ClickableSpan() {                @Override                public void onClick(View widget) {                    //Do something.                    if(isAdded()) {                        Toast.makeText(getActivity(), "You have clicked!", Toast.LENGTH_LONG).show();//                        avoidHintColor(widget);                    }                }                @Override                public void updateDrawState(@NonNull TextPaint ds) {                    super.updateDrawState(ds);                    ds.setColor(getResources().getColor(android.R.color.holo_red_dark));                    ds.setUnderlineText(false);                    ds.clearShadowLayer();                }            };            spannableString.setSpan(clickableSpan,0,str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);            textView.setText(spannableString);            textView.setMovementMethod(LinkMovementMethod.getInstance());        }        private void avoidHintColor(View view){            if(view instanceof TextView)                ((TextView)view).setHighlightColor(getResources().getColor(android.R.color.transparent));        }    }}        

 

會出現文字選中出現淡綠色的背景色現象。如1.1。ds.setColor()設定的是span超連結的文本顏色,而不是點擊後的顏色,點擊後的背景顏色(HighLightColor)屬於TextView的屬性,Android4.0以上預設是淡綠色,低版本的是黃色。

   解決方案就是通過

((TextView)view).setHighlightColor(getResources().getColor(android.R.color.transparent));方法重新設定文字背景為透明色。

修改後的結果1.2。

圖1.1:

 

 

圖1.2:

 

android中TextView 添加ClickableSpan後點擊選中文字背景問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.