我必须将A和B写入编辑文本的按钮。如果edittext中有什么内容,我怎么能用“Del”按钮删除最后一个字母?
我的布局:
<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;
}
}
}
我该怎么办?谢谢客栈提前。
答案 0 :(得分:29)
您可以检索EditText
的文字,然后获取该文字的sub-string
,然后将该文字重新设置为EditText
,如下所示......
String text = editText.getText().toString();
editText.setText(text.substring(0, text.length() - 1));
您还可以使用以下程序....它会更有效率。
int length = editText.getText().length();
if (length > 0) {
editText.getText().delete(length - 1, length);
}
您应该使用switch-case
,如下所示...并按照以下方式处理您的nuttondel
onclick()...
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;
}
}
}
答案 1 :(得分:4)
使用substring()方法,你可以做到,
String str = myEditText.getText().toString();
str = str.substring ( 0, str.length() - 1 );
// Now set this Text to your edit text
myEditText.setText ( str );
你需要在onClick()方法中写上面的行。
答案 2 :(得分:0)
str=edittext.gettext+"";
str.length();
然后循环
或str1 = str.substring ( 0, str.length() - 1 );