I want to set text or add text to a specific line in the EditText in android? Are there any methods available?
As I know setText
will overwrite the whole text and append
will add text at the end, is there any specific method for what I want to do?
I know how to read and get a text from specific line, but how to make changes to that text without disturbing other lines below and above it?
1) get the Editable
from EditText
using EditText#getText();
2) do your modifications to the Editable
you've get in previous step.
Editable
has a method insert(int where, CharSequence text)
- use it to insert where you want.
3) set it back to the EditText
using EditText#setText();
(SET THE WHOLE MODIFIED Editable
BACK)
If you want to know where there are end of line signs in the Editable
you received then search it for the new line char aka '\\n', then use this place/places to insert somethig after the '\\n'
, if you want to insert in a particular line that is alligned by EditText, then you need to get to the "alligning engine" of EditText
, I dont't know the details how this works, so you need to read the sources if you want to do that.
You should try using this type code
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);
and set button onClick to add new character which you want
menu1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
edt_Text.getText().insert(6, "are");
}
});
this code is for specific character base
now you should find line from character and insert text using this method.
i hope this will help you.
Most probably the multi line text will be separated by a line breaks and the below code will give you a clarification
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);
}
};
}
And I f you wanna add the content in edit text you can add by getting the EditText
value and append the string whatever you need and you can set it back like below
etMultiLine.setText("Edited Text");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.