私は、ボタンを使って太字のテキストを追加する機能を持つノート作成アプリケーションを作成しています。太字のボタンをクリックすると太字モードがオンになります。つまり、EditTextのユーザーから取り込まれたテキストが太字になります。次へ太字ボタンをクリックすると、太字モードがオフになります。つまり、ユーザーから取り込まれたテキストが標準タイプになります。AndroidでEditTextで太字を入力する方法は?
この投稿を見ました。 Change future text to bold in EditText in Android しかし、その投稿の回答はうまくいかなかった。 ボタンを最初にクリックすると大胆な入力が行われますが、ボタンをもう一度クリックして入力として通常のテキストを入力すると、入力として太字のテキストが取り込まれていました。ここで
はコードです: TextArea.javapackage 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:TEXTSTYLE = "大胆:
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);
}
}
});
}
}
またはhttps://github.com/wasabeef/richheditor-android –
私はその複雑なRichTextEditorは必要ありません私はちょうど私のEditTextビューを太字の入力を取るしたい。 –
HTMLまたは文字列のスパニング可能なセクションを作成する必要があります。現在の回答は、入力ボックス全体を太字にしています。返信いただきありがとうございます。 –