所以我有一个 android 应用程序(Eclipse 中的 java),我想通过 shift 方法更改键盘上某些按钮的文本。我实现了与更改 textview 文本相同的代码,就像每个人在类似问题上所说的一样,但由于某种原因它不起作用。出于某种原因,在测试了其他按钮功能之后,我确定它不喜欢我更改按钮的任何属性。尝试清理项目,没有帮助。不断收到调用异常。以下是相关代码:
public class MainActivity extends Activity {
boolean shift = true;
static Vector<String> answer = new Vector<String>(1, 1);
static int ansLength = 0;
private TextView answerbox;
private Button a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initializeButtons();
setContentView(R.layout.activity_main);
answerbox = (TextView) findViewById(R.id.answerbox);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void initializeButtons() {
a = (Button) findViewById(R.id.a);
}
public void typeKey(View sender) {
Button pressed = (Button) sender;
answer.add(ansLength, (String) pressed.getText());
//answerbox.setText("test string");
ansLength++;
StringBuilder stringBuilder = new StringBuilder();
for (String string : answer) {
stringBuilder.append(string);
}
answerbox.setText(stringBuilder.toString());
}
public void backSpace(View sender) {
answer.remove(ansLength - 1);
ansLength--;
StringBuilder stringBuilder = new StringBuilder();
for (String string : answer) {
stringBuilder.append(string);
}
answerbox.setText(stringBuilder.toString());
}
public void shift(View sender) {
if (shift == true) {
shift = false;
a.setText("l");
}
}
}
下面的 XML:
<Button
android:id="@+id/a"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="A"
android:onClick="typeKey"/>
<Button
android:id="@+id/shift1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="^"
android:textSize="24sp"
android:onClick="shift" />