Android 是否可以将按钮强制转换为文本视图?
我有两个按钮:Android 是否可以将按钮强制转换为文本视图?,android,android-view,textview,android-button,Android,Android View,Textview,Android Button,我有两个按钮: <Button android:id="@+id/fragment_remote_control_zeroButton" android:layout_width="0dp" android:layout_height="match_parent" android:text="0" /> <Button android:id="@+id/fragment_remote_control_oneButton" andr
<Button
android:id="@+id/fragment_remote_control_zeroButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="0" />
<Button
android:id="@+id/fragment_remote_control_oneButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="1" />
在侦听器的onClick(View v)
方法中,方法参数是单击的视图
,这是单击的按钮
但我想知道它是如何将一个按钮投射到文本视图的???这是指按钮中的文本值还是不是???
Button实际上是TextView的一个子类
但是,代码肯定很奇怪,为了确定单击了哪个按钮,您可以检查id
e、 g
你想达到什么目的?@UdiI有两个文本视图,一个是当我点击上面的两个按钮时,它的值会出现在其中一个按钮上,然后如果我按enter键,它的值会显示在另一个文本视图上,如果按Delete键,它会重置第一个文本视图对不起,但我不明白你的确切意思。如果你能解释得更详细一点,我会尽力帮助你。我用setOnClickListener BB分别为这两个设置了listener,但这里没有显示,我的主要问题是将一个按钮强制转换为TextView,所以我告诉过你,Button是TextView的一个子类,所以收到的
视图v
是一个按钮,因此是一个TextView,所以,就像TextView是一个视图一样,Button实际上是Android中的一个TextView,这里String text=TextView.getText().toString()代码>文本变量的值是多少???android:text=
值???
View.OnClickListener numberButtonListener = new View.OnClickListener() {
public void onClick(View v) {
TextView textView = (TextView)v;
String working = mWorkingTextView.getText().toString();
String text = textView.getText().toString();
if (working.equals("0")) {
mWorkingTextView.setText(text);
} else {
mWorkingTextView.setText(working + text);
}
}
};
if (v.getId() == R.id.fragment_remote_control_zeroButton) {
mWorkingTextView.setText(text);
} else {
mWorkingTextView.setText(working + text);
}