Android 如何使用按钮从 EditText 中删除最后一个字母?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22316563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:49:15  来源:igfitidea点击:

How to delete the last letter from EditText with button?

androidandroid-edittextandroid-button

提问by Tamas Koos

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 ?

我必须将 A 和 B 写入编辑文本的按钮。如果编辑文本中有内容,如何使用“Del”按钮删除最后一个字母?

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:

还有我的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.

我该怎么做?提前感谢客栈。

回答by Hamid Shatu

You can retrieve the text of EditTextand then get the sub-stringof that text and set again that text to EditTextas below...

您可以检索文本,EditText然后获取该sub-string文本的文本并再次将该文本设置EditText为如下...

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-caseas below...and handle your nuttondelonclick() as follows...

您应该使用switch-case如下...并按如下方式处理您的nuttondelonclick()...

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

    }

}

回答by user2060383

Using substring() method , you can do it,

使用 substring() 方法,你可以做到,

String str = myEditText.getText().toString();
str = str.substring ( 0, str.length() - 1 );
// Now set this Text to your edit text
myEditText.setText ( str );

you need to write above lines in onClick() method.

您需要在 onClick() 方法中编写以上几行。

回答by Android

str=edittext.gettext+"";
str.length();

then for loop or str1 = str.substring ( 0, str.length() - 1 );

然后 for 循环或 str1 = str.substring ( 0, str.length() - 1 );