安卓的EditText - 删除最后一个字母一个按钮 [英] Android EditText - Delete the last letter with a button
本文介绍了安卓的EditText - 删除最后一个字母一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要的按钮写入A和B到一个EditText。如果有什么东西在我的EditText如何删除最后一个字母的删除按钮? 我的布局:
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>
和我的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 make it. Please help me! Thanks in advance.
推荐答案
是的,你可以创建一个onClickListener和刚刚获得来自编辑文本的文本,并删除最后一个字符。
Yes you can create an onClickListener and just get the text from 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
}
}
});
}
编辑:也不要忘记检查字符串的长度是在事件a或b这样做没有得到pressed当用户点击删除之前大于0
Also don't forget to check the string length is greater than 0 before doing this in the event a or b hasn't been pressed when the user hits delete.
这篇关于安卓的EditText - 删除最后一个字母一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文