【发布时间】:2018-04-08 13:25:33
【问题描述】:
我想制作一个类似于退格的按钮,但我找不到完美的代码,就像我制作了一个按钮,它使用 append() 方法在一行代码中将某些字符添加到 Textview,而不是制作多行的东西。 所以我的问题是:有没有办法在退格按钮代码中做到这一点,我知道我可以制作多行/长代码,但是与 append() 有相反的情况吗?
【问题讨论】:
标签: java android android-studio calculator backspace
我想制作一个类似于退格的按钮,但我找不到完美的代码,就像我制作了一个按钮,它使用 append() 方法在一行代码中将某些字符添加到 Textview,而不是制作多行的东西。 所以我的问题是:有没有办法在退格按钮代码中做到这一点,我知道我可以制作多行/长代码,但是与 append() 有相反的情况吗?
【问题讨论】:
标签: java android android-studio calculator backspace
这适用于字符退格:
buttonBackspace.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String word = editText.getText().toString();
int input = word.length();
if (input > 0){
editText.setText(word.substring(0, input-1));
}
}
});
【讨论】:
使用退格键删除字符。
首先初始化视图
Button BackSpaceButton;
然后找到视图
BackSpaceButton = findViewById(R.id.BackBtn);
然后给按钮添加监听器
BackSpaceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String word = EditTextInputNumbers.getText().toString();
int input = word.length();
if (input > 0) {
EditTextInputNumbers.setText(word.substring(0, input - 1));
}
}
});
【讨论】: