<i id='WnW5T'><tr id='WnW5T'><dt id='WnW5T'><q id='WnW5T'><span id='WnW5T'><b id='WnW5T'><form id='WnW5T'><ins id='WnW5T'></ins><ul id='WnW5T'></ul><sub id='WnW5T'></sub></form><legend id='WnW5T'></legend><bdo id='WnW5T'><pre id='WnW5T'><center id='WnW5T'></center></pre></bdo></b><th id='WnW5T'></th></span></q></dt></tr></i><div id='WnW5T'><tfoot id='WnW5T'></tfoot><dl id='WnW5T'><fieldset id='WnW5T'></fieldset></dl></div>

    • <bdo id='WnW5T'></bdo><ul id='WnW5T'></ul>
    <tfoot id='WnW5T'></tfoot>
  1. <legend id='WnW5T'><style id='WnW5T'><dir id='WnW5T'><q id='WnW5T'></q></dir></style></legend>

      <small id='WnW5T'></small><noframes id='WnW5T'>

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

      时间:2023-10-01

      <legend id='GfWdi'><style id='GfWdi'><dir id='GfWdi'><q id='GfWdi'></q></dir></style></legend>

      • <i id='GfWdi'><tr id='GfWdi'><dt id='GfWdi'><q id='GfWdi'><span id='GfWdi'><b id='GfWdi'><form id='GfWdi'><ins id='GfWdi'></ins><ul id='GfWdi'></ul><sub id='GfWdi'></sub></form><legend id='GfWdi'></legend><bdo id='GfWdi'><pre id='GfWdi'><center id='GfWdi'></center></pre></bdo></b><th id='GfWdi'></th></span></q></dt></tr></i><div id='GfWdi'><tfoot id='GfWdi'></tfoot><dl id='GfWdi'><fieldset id='GfWdi'></fieldset></dl></div>

            <tfoot id='GfWdi'></tfoot>

            <small id='GfWdi'></small><noframes id='GfWdi'>

            • <bdo id='GfWdi'></bdo><ul id='GfWdi'></ul>
                  <tbody id='GfWdi'></tbody>
                本文介绍了如何使用按钮从 EditText 中删除最后一个字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

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

                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 ?

                我的布局:

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

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

                推荐答案

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

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

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

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

                这篇关于如何使用按钮从 EditText 中删除最后一个字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:获取 EditText 的字符数 下一篇:JUnit 混淆:使用“扩展 TestCase"或“@Test"?

                相关文章

                最新文章

              • <tfoot id='Olyrg'></tfoot>
              • <legend id='Olyrg'><style id='Olyrg'><dir id='Olyrg'><q id='Olyrg'></q></dir></style></legend>

                  <i id='Olyrg'><tr id='Olyrg'><dt id='Olyrg'><q id='Olyrg'><span id='Olyrg'><b id='Olyrg'><form id='Olyrg'><ins id='Olyrg'></ins><ul id='Olyrg'></ul><sub id='Olyrg'></sub></form><legend id='Olyrg'></legend><bdo id='Olyrg'><pre id='Olyrg'><center id='Olyrg'></center></pre></bdo></b><th id='Olyrg'></th></span></q></dt></tr></i><div id='Olyrg'><tfoot id='Olyrg'></tfoot><dl id='Olyrg'><fieldset id='Olyrg'></fieldset></dl></div>

                  <small id='Olyrg'></small><noframes id='Olyrg'>

                      <bdo id='Olyrg'></bdo><ul id='Olyrg'></ul>