如何在Android中的EditText中输入粗体字符作为输入?

时间:2017-05-29 05:42:57

标签: android android-edittext

我正在制作一个Note Making App,它具有通过按钮添加粗体文本的功能。单击粗体按钮时,应打开粗体模式,即从EditText中的用户获取的文本现在应该是粗体。在下一个单击粗体按钮时,应关闭粗体模式,即从用户处取出的文本现在应为普通类型。

我看过这篇文章。 Change future text to bold in EditText in Android 但那篇文章的答案没有成功。 当我第一次点击按钮时,它采用粗体输入,但当我再次单击该按钮以将普通文本作为输入时,它仍然以粗体文本作为输入。

这是代码: TextArea.java

package com.example.syed.andtexteditor;


import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.AppCompatEditText;
import android.text.Spannable;
import android.text.style.StyleSpan;
import android.util.AttributeSet;
import android.widget.EditText;


public class TextArea extends AppCompatEditText {

    public static final int TYPEFACE_NORMAL = 0;
    public static final int TYPEFACE_BOLD = 1;
    public static final int TYPEFACE_ITALICS = 2;
    public static final int TYPEFACE_BOLD_ITALICS = 3;

    private int currentTypeface;
    private int lastCursorPosition;
    private int tId;


    public TextArea(Context context) {
        super(context);
        lastCursorPosition = this.getSelectionStart();
    }

    public TextArea(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    public int gettId() {
        return tId;
    }

    public void settId(int tId) {
        this.tId = tId;
    }

    public void changeTypeface(int tfId) {
        currentTypeface = tfId;
        lastCursorPosition = this.getSelectionStart();
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        Spannable str = this.getText();
        StyleSpan ss;
        int endLength = text.toString().length();

        switch(currentTypeface) {
            case TYPEFACE_NORMAL:
                ss = new StyleSpan(Typeface.NORMAL);
                break;
            case TYPEFACE_BOLD:
                ss = new StyleSpan(Typeface.BOLD);
                break;
            case TYPEFACE_ITALICS:
                ss = new StyleSpan(Typeface.ITALIC);
                break;
            case TYPEFACE_BOLD_ITALICS:
                ss = new StyleSpan(Typeface.BOLD_ITALIC);
                break;
            default:
                ss = new StyleSpan(Typeface.NORMAL);
        }

        str.setSpan(ss, lastCursorPosition, endLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

MainActivity.java:

package com.example.syed.andtexteditor;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class MainActivity extends AppCompatActivity {
    boolean boldClicked=false;
    int typefaceStyle = TextArea.TYPEFACE_NORMAL;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button boldButton = (Button)findViewById(R.id.bold_button);
        boldButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TextArea t =  (TextArea) findViewById(R.id.textInput);
                if (!boldClicked)
                    boldClicked = true;
                else if (boldClicked)
                    boldClicked = false;
                if (boldClicked) {
                    typefaceStyle = TextArea.TYPEFACE_BOLD;
                    t.changeTypeface(typefaceStyle);
                }
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

将此行添加到布局的Editext

  

机器人:TEXTSTYLE = “黑体”