Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete the last letter from EditText with button? [duplicate]

I have to buttons that writes A and B to an edittext. If there is something in the edittext how can I 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, buttondel;
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);

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


   }

public void onClick(View v) {
switch(v.getId()) {
    case R.id.buttona : 
        Button buttona = (Button)v;
    editText.setText(editText.getText().toString()+buttona.getText().toString());
        break;
    case R.id.buttondel :
        String text = editText.getText().toString();
        editText.setText(text.substring(0, text.length() - 1));
        break;



}

}


}

How can I do it? Thanks inn advance.

like image 332
Tamas Koos Avatar asked Mar 11 '14 04:03

Tamas Koos


People also ask

How to remove/delete already typed text inside edittext using edittext?

How to remove/delete already typed text inside edittext to empty edittext on button click dynamically. In this tutorial we are programmatically removing the typed text from EditText so application user doesn’t need to remove text by hand back press keypad key. This will be possible using getText ().clear () method.

How to clear edittext in Android on button click programmatically?

This will be possible using getText ().clear () method. This method will first get the already typed text from edittext and then clear it. So here is the complete step by step tutorial for Clear EditText in android on button click programmatically.

How do I remove the last letter of a string?

A simple regular expression of “.$” will match the last character in a string. The dollar sign means match from the end of the string, and a period (or dot) means any single character. Therefore, the following command will replace the last character with nothing, and effectively remove the last letter of the string: PS C:\> $string -replace “.$”

How to remove edittext underline programmatically in Android?

Click here to download Clear EditText in android on button click programmatically project with source code. Related Posts. Move EditText layout just above soft keyboard when it shows in android. Open new activity on button click in android by existing activity. Remove EditText underline programmatically in android.


1 Answers

You can retrieve the text of EditText and then get the sub-string of that text and set again that text to EditText as below...

String text = editText.getText().toString();
editText.setText(text.substring(0, text.length() - 1));

You can also use following procedure....it will be more efficient.

int length = editText.getText().length();
if (length > 0) {
    editText.getText().delete(length - 1, length);
}

You should use switch-case as below...and handle your nuttondel onclick() as follows...

public class MainActivity extends Activity implements OnClickListener {

    Button buttona, buttonb;
    Button buttonDel;
    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);

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

    buttonDel = (Button) findViewById(R.id.buttondel);
    buttonDel.setOnClickListener(this);


   }

    public void onClick(View v) {

       switch(v.getId()) {

          case R.id.buttona:
                      editText.setText(editText.getText().toString()+buttona.getText().toString());
              break;

          case R.id.buttonb:
                      editText.setText(editText.getText().toString()+buttonb.getText().toString());
              break;

          case R.id.buttondel:

              int length = editText.getText().length();
              if (length > 0) {
                  editText.getText().delete(length - 1, length);
              }
              break;
       }

    }

}
like image 80
Hamid Shatu Avatar answered Sep 22 '22 16:09

Hamid Shatu