Android layout 为什么按钮不起作用?
有人能解释一下怎么了吗 我希望它能像这样工作: 屏幕顶部有三个按钮。当我按下按钮1时,它应该显示使#1可见。当我按下按钮2时,它会显示#2并隐藏#1和#3,依此类推。。。我有不同的布局在每个 片段类:Android layout 为什么按钮不起作用?,android-layout,android-button,Android Layout,Android Button,有人能解释一下怎么了吗 我希望它能像这样工作: 屏幕顶部有三个按钮。当我按下按钮1时,它应该显示使#1可见。当我按下按钮2时,它会显示#2并隐藏#1和#3,依此类推。。。我有不同的布局在每个 片段类: <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@c
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dynamik"
android:id="@+id/button1"
android:layout_weight="1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Toner"
android:id="@+id/button2"
android:layout_weight="1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Takter"
android:id="@+id/button3"
android:layout_weight="1"/>
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/scrollView">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
android:id="@+id/symb_id1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
layout="@layout/fragment_musikteori_symb_dynamik" />
<include
android:id="@+id/symb_id2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
layout="@layout/fragment_musikteori_symb_toner" />
<include
android:id="@+id/symb_id3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
layout="@layout/fragment_musikteori_symb_takter" />
</FrameLayout>
</ScrollView>
</LinearLayout>
布局:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dynamik"
android:id="@+id/button1"
android:layout_weight="1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Toner"
android:id="@+id/button2"
android:layout_weight="1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Takter"
android:id="@+id/button3"
android:layout_weight="1"/>
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/scrollView">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
android:id="@+id/symb_id1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
layout="@layout/fragment_musikteori_symb_dynamik" />
<include
android:id="@+id/symb_id2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
layout="@layout/fragment_musikteori_symb_toner" />
<include
android:id="@+id/symb_id3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
layout="@layout/fragment_musikteori_symb_takter" />
</FrameLayout>
</ScrollView>
</LinearLayout>
我没有看到单击操作的按钮注册。
和其他一些错误:)
请查看以下教程: 你需要这样的东西:
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
或者,如果您实现了单击侦听器,那么您可以使用
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
在XML层中使用@+id/button1
。。然后在代码中,我认为这是错误的,然后修复
View.GONE
和switch
命令。。
尝试一些教程,然后再试一次。如果在此处找到解决方案:
对于您设置的第一个按钮
View.go
适用于所有情况,是否正确?(只注意,不解决:)。。您不需要这个部分:}开关(v.getId()){
用于按钮2和按钮3部分-因此,您可以使用如下代码:开关(v.getId()){case x:some_code;break;case y:some_code;break;}
我不知道代码出了什么问题,但为什么不解释它出了什么问题?比如,发生了什么?你看到了什么?例如,如果唯一的问题是按钮一不起作用,你应该检查@Atiris的注释。如果有更多的问题,为什么不解释?当我按下按钮时,什么都不会发生,这就是问题所在。我正在尝试他/她现在解决方案。@Atiris它仍然不起作用:(您没有设置onClick侦听器,请参阅@Atiris answer。谢谢,我已经更新了我的帖子,但是当我使用您的代码时,它会强制关闭,即使没有任何附加到按钮的操作。我现在添加了它OK,这一行有什么错误:com.okramuf.musikteori.musikteori\u main.onCreate(musikteori_main.java:51)
?或者尝试从片段中删除任何代码1。然后打开/启动应用程序,您可以看到按钮?
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);