从Edittext粗体和非粗体中选择文本

时间:2015-11-19 12:43:28

标签: java android

我正在尝试为我的应用制作一个功能,用户可以在其中制作选定的文字Bold,但我也希望这样做,以便如果所选文本已经是粗体,那么文本将再次设置为正常,一种撤消功能。

我已经尝试了100多种没有任何傻瓜的组合!

    CharacterStyle csBold = new StyleSpan(Typeface.BOLD);
    CharacterStyle csNormal = new StyleSpan(Typeface.NORMAL);
    int start = editText.getSelectionStart();
    int end = editText.getSelectionEnd();
    SpannableStringBuilder ssb = new SpannableStringBuilder(editText.getText());
    ssb.setSpan(csBold, start, end, 1);
    SpannableStringBuilder ssb2 = new SpannableStringBuilder(editText.getText());
    ssb2.setSpan(csNormal, start, end, 1);



    switch(item.getItemId()) {

        case R.id.bold:

            while(editText.isSelected()){

            if(editText.getSelectionStart() + editText.getSelectionEnd() == ssb.getSpanStart(start) + ssb.getSpanEnd(end)){
                editText.setText(ssb2);
                return true;

            }else{
                editText.setText(ssb);
                return true;

            }

            }

1 个答案:

答案 0 :(得分:1)

更新答案

    Edittext editext;
    Button bold_btn , normal_btn , itlac;
   CharacterStyle styleBold  , styleItalc;
   boolean bold = false;

        @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                setContentView(R.layout.activity_main);
                styleBold = new StyleSpan(Typeface.BOLD);
                styleNormal = new StyleSpan(Typeface.NORMAL);
                styleItalc = new StyleSpan(Typeface.ITALIC);
                underLine = new UnderlineSpan();
                editext = (Editext) findViewById(R.id.editext);
               bold_btn = (Button) findViewById(R.id.button1);
              italic_btn = (Button) findViewById(R.id.button2);
              italic_btn.setOnClickListener(new View.OnClickListener() {
               @Override
              public void onClick(View v) {
            String wholeText = username.getText().toString();
            int start = username.getSelectionStart();
            int end = username.getSelectionEnd();

            SpannableStringBuilder sb = new SpannableStringBuilder(wholeText);

            sb.setSpan(styleItalic, start, end, 0);
            username.setText(sb);
        }
    });
              ........
               .....
               bold_btn.setOnClickListener(BoldbuttonListener);
               normal_btn.setOnClickListener(normalbuttonListener);


    }



            View.OnClickListener BoldbuttonListener = new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {


                        bold = !bold;

        String wholeText = username.getText().toString();
        int start = username.getSelectionStart();
        int end = username.getSelectionEnd();

        CharacterStyle passedStyle;
        SpannableStringBuilder sb = new SpannableStringBuilder(wholeText);
        if(bold) {
            passedStyle = styleNormal;


        }else {
            passedStyle = styleBold;

        }
        sb.setSpan(passedStyle, start, end, 0);
        editext.setText(sb);


                    }
                };

正常效果相同