[英]How to set text or add text to a specific line in multiline EditText in android?
我想在Android 的EditText中設置文本或將文本添加到特定行 ? 有沒有可用的方法?
據我所知, setText
會覆蓋整個文本,而append
將在末尾添加文本,我想要做什么特定的方法?
我知道如何從特定的行中讀取和獲取文本,但是如何在不干擾該行上下的其他行的情況下對該文本進行更改?
1)使用EditText#getText();
從EditText
獲取Editable
EditText#getText();
2)對您在上一步中獲得的Editable
進行修改。
Editable
有一個方法insert(int where, CharSequence text)
-使用它可以在想要的地方插入。
3)使用EditText#setText();
將其設置回EditText
EditText#setText();
(組修改的全體Editable
BACK)
如果您想知道收到的Editable
中行尾符號的位置,然后在其中搜索新行字符aka'\\ n',然后使用該位置在'\\n'
之后插入somethig。在通過的EditText神韻:一個特定的行插入,那么你需要去的“alligning引擎” EditText
,我dont't知道的細節是如何工作的,所以你需要,如果你想要做的是讀取源。
您應該嘗試使用此類型代碼
EditText edt_Text= new EditText(this);
edt_Text.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
edt_Text.setText("Hi how you");
lnrImageButton.addView(edt_Text);
並設置按鈕onClick添加所需的新字符
menu1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
edt_Text.getText().insert(6, "are");
}
});
該代碼用於特定的字符庫
現在您應該從字符中找到行,並使用此方法插入文本。
我希望這能幫到您。
多行文本很可能用換行符分隔,下面的代碼將給您澄清
public class EditTextExample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edtittext_example);
Button button = (Button) findViewById(R.id.m_button);
button.setOnClickListener(btnListener);
}
//button listener
private OnClickListener btnListener = new OnClickListener() {
public void onClick (View view) {
EditText etMultiLinetext = (EditText) findViewById(R.id.etMultiLineText);
String str = etMultiLinetext.getText().toString();
Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();
TextView tvMultiLineText = (TextView) findViewById(R.id.tvMultiLineText);
String[] strArr = str.split(System.getProperty("line.separator"));
for (int i = 0; i < strArr.length; i++) {
Log.e("String value", "String value" + strArr[i]);
}
tvMultiLineText.setText(str);
}
};
}
如果您想在編輯文本中添加內容,則可以通過獲取EditText
值來添加內容,並根據需要添加字符串,然后可以將其設置為如下所示
etMultiLine.setText("Edited Text");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.