TextView in Android adds ClickableSpan to click on the selected text background discoloration problem

setHighlightColor(int color) in TextView is used to set the background color of the selected text to be highlighted.

 For example the following:

 

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

 

There will be a light green background color phenomenon when the text is selected. As shown in Figure 1.1 below. ds.setColor() sets the text color of the span hyperlink, not the clicked color. The clicked background color (HighLightColor) belongs to the attributes of TextView. The default is light green for Android 4.0 and above, and yellow for lower versions. .

   The solution is to pass

((TextView)view).setHighlightColor(getResources().getColor(android.R.color.transparent)); method resets the text background to transparent color.

 The modified result is shown in Figure 1.2.

Figure 1.1:

 

 

Figure 1.2:

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326644924&siteId=291194637