• Android中TextView 添加ClickableSpan后点击选中文字背景变色问题


    TextView中的setHighlightColor(int color)用于设置选中文字背景色高亮显示。

     比如以下:

    public class MainActivity extends ActionBarActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .add(android.R.id.content, 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:

  • 相关阅读:
    hdu1124 Factorial (求解一个数的阶乘中出现多少个0)
    SQL on Linux: Erro Unable to read instance id from /var/opt/mssql/.system/instance_id
    Error during WebSocket handshake: Unexpected response code: 200 问题处理
    CUP计算资源争抢通过IIS启用处理器关联解决
    ABP在MultipleDbContext也就是多库的场景下发布后异常“Could not find content root folder”问题处理
    ABP运行Login failed for user 'IIS APPPOOL XXXXX Reason: Could not find a login matching the name provided问题解决
    vs2017cpu占用过高解决方案
    docker查看挂载目录Volume
    windows 10安装docker一直挂起在Installing Components and Removing Files
    ABP vue+asp.net core yarn serve报 Cannot find module 'typescript/package.json错误
  • 原文地址:https://www.cnblogs.com/sxzheng/p/4245873.html
Copyright © 2020-2023  润新知