Android EditText - delete last letter with button

I have buttons that write A and B in edittext. If there is something in the edittext, how to delete the last letters with the "Del" button? My layout:

<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>

And my java:

package com.koostamas.keyboard;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;

public class MainActivity extends Activity implements OnClickListener {

Button buttona, buttonb;
EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);

addListenerOnButton();
}


   public void addListenerOnButton() {

buttona = (Button) findViewById(R.id.buttona);
buttona.setOnClickListener(this);
buttona.getText();
buttonb = (Button) findViewById(R.id.buttonb);
buttonb.setOnClickListener(this);
buttonb.getText();

   }

public void onClick(View v) {

    Button buttona = (Button)v;
    editText.setText(editText.getText().toString()+buttona.getText().toString());


}

public void onClick1(View v) {

    Button buttonb = (Button)v;
    editText.setText(editText.getText().toString()+buttonb.getText().toString());


}
}

So I want to do this. Please help me! Thanks in advance.

+4
source share
4 answers

Yes, you can create onClickListener and just get the text from the edit text and delete the last character.

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
         }
      }
   });
}

Edit: also remember to check that the string length is greater than 0 before doing this in the case where a or b were not pressed when the user hit.

+6
source

Installing new text # 1

CharSequence text = edittext.getText();
edittext.setText(text.subSequence(0, text.length() - 1));

# 2

CharSequence text = v.getText();
edittext.setText("");
edittext.append(text, 0, text.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);
}

.

edittext.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
+1
String text = editText.getText().toString();
if(text.length() > 0){
  text = str.substring(0, str.length()-1);
   editText.setText(text);
}
0
source

one limitation of other answers is if the cursor is not in the last position, and then it deletes the last character, which is eliminated in this answer Write down when you click on the button

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
source

Source: https://habr.com/ru/post/1531022/


All Articles