Java Android EditText-使用按钮删除最后一个字母
我必须使用将A和B写入编辑文本的按钮。如果编辑文本中有内容,如何使用“删除”按钮删除最后的字母? 我的布局:Java Android EditText-使用按钮删除最后一个字母,java,android,android-edittext,android-button,Java,Android,Android Edittext,Android Button,我必须使用将A和B写入编辑文本的按钮。如果编辑文本中有内容,如何使用“删除”按钮删除最后的字母? 我的布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_paren
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/buttonb"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/buttona"
android:text="@string/buttonb"
android:textSize="50sp"
android:textStyle="bold" />
<Button
android:id="@+id/buttona"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/buttona"
android:textSize="50sp"
android:textStyle="bold" />
<Button
android:id="@+id/buttondel"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="@string/buttondel"
android:textSize="50sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="58"
android:textSize="20sp"
android:textStyle="bold"
android:inputType="text" >
<requestFocus />
</EditText>
</RelativeLayout>
所以我想成功。请帮帮我!
提前感谢。是的,您可以创建onClickListener,只需从编辑文本中获取文本并删除最后一个字符
String text = editText.getText().toString();
if(text.length() > 0){
text = str.substring(0, str.length()-1);
editText.setText(text);
}
Button delete = (Button) findViewById(R.id.buttondel);
if( delete != null ) {
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick() {
String textString = editText.getText().toString();
if( textString.length() > 0 ) {
editText.setText(textString.substring(0, textString.length() - 1 ));
editText.setSelection(editText.getText().length());//position cursor at the end of the line
}
}
});
}
编辑:如果用户点击“删除”按钮时未按下a或b,则在执行此操作之前,也不要忘记检查字符串长度是否大于0。设置新文本#1
设置新文本#2
删除可编辑的
int length = editText.getText().length();
if (length > 0) {
editText.getText().delete(length - 1, length);
}
int length = editText.getText().length();
if (length > 0) {
editText.getText().subSequence(0, length - 1);
}
子序列可编辑
int length = editText.getText().length();
if (length > 0) {
editText.getText().delete(length - 1, length);
}
int length = editText.getText().length();
if (length > 0) {
editText.getText().subSequence(0, length - 1);
}
通过调度delete key事件进行删除
edittext.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
其他答案的一个限制是,如果光标不在最后一个位置,那么它也会删除此答案中删除限制的最后一个字符 按一下按钮写下来
int end = ((EditText)findViewById(R.id.one)).getSelectionEnd();
if(end>0){
SpannableStringBuilder selectedStr = new SpannableStringBuilder(((EditText) findViewById(R.id.one)).getText());
selectedStr.replace(end - 1, end, "");
((EditText) findViewById(R.id.one)).setText(selectedStr);
((EditText) findViewById(R.id.one)).setSelection(end-1);
}
子字符串的结束索引是独占的。所以如果你说子串(0,1),结果中只有索引0。所以-1是正确的。请看Gabe对你的其他评论子串的回复是独占的,所以你需要-1而不是-2。请告诉我应该把它放在哪里。我来不了。谢谢。在你的onCreateMethod中,你需要找到你的删除按钮,就像你对ButtonA和ButtonB所做的那样,然后在该按钮上调用setOnClick listener,如上所示。我编辑了响应以显示该步骤
int end = ((EditText)findViewById(R.id.one)).getSelectionEnd();
if(end>0){
SpannableStringBuilder selectedStr = new SpannableStringBuilder(((EditText) findViewById(R.id.one)).getText());
selectedStr.replace(end - 1, end, "");
((EditText) findViewById(R.id.one)).setText(selectedStr);
((EditText) findViewById(R.id.one)).setSelection(end-1);
}