所以我想将OnClick方法连接到按钮A,但是当我点击该按钮A时,我想要它,其他按钮(B,C,D,F)将变得可见。但是当我运行代码时,任何人都可以帮助我吗?
public void OnClick(View v)
{
b.setVisibility(View.VISIBLE);
c.setVisibility(View.VISIBLE);
d.setVisibility(View.VISIBLE);
f.setVisibility(View.VISIBLE);
}
我将此方法添加到activity_main.xml中的按钮A。
答案 0 :(得分:0)
您的第一个按钮应如下所示:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/checkbox_off_background"
android:layout_marginLeft="62dp"
android:layout_marginStart="62dp"
android:layout_marginTop="15dp"
android:id="@+id/imgB1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onClick" />
根据最后一行,您的MainActivity中的方法应如下所示(使用OnClick小写o)
public void onClick(View v) {
op1.setVisibility(View.VISIBLE);
op2.setVisibility(View.VISIBLE);
op3.setVisibility(View.VISIBLE);
op4.setVisibility(View.VISIBLE);
op5.setVisibility(View.VISIBLE);
op6.setVisibility(View.VISIBLE);
op7.setVisibility(View.VISIBLE);
op8.setVisibility(View.VISIBLE);
}
如果没有错误日志,我们无法比您更准确地帮助您
答案 1 :(得分:0)
使用此完整代码:
ImageButton imB1, op1, op2, op3, op4, op5, op6, op7, op8;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imB1 = (ImageButton) findViewById(R.id.imgB1);
op1 = (ImageButton) findViewById(R.id.esc1);
op2 = (ImageButton) findViewById(R.id.esc2);
op3 = (ImageButton) findViewById(R.id.esc3);
op4 = (ImageButton) findViewById(R.id.esc4);
op5 = (ImageButton) findViewById(R.id.esc5);
op6 = (ImageButton) findViewById(R.id.esc6);
op7 = (ImageButton) findViewById(R.id.esc7);
op8 = (ImageButton) findViewById(R.id.esc8);
}
public void Clicked(View v) {
op1.setVisibility(View.VISIBLE);
op2.setVisibility(View.VISIBLE);
op3.setVisibility(View.VISIBLE);
op4.setVisibility(View.VISIBLE);
op5.setVisibility(View.VISIBLE);
op6.setVisibility(View.VISIBLE);
op7.setVisibility(View.VISIBLE);
op8.setVisibility(View.VISIBLE);
}
并在xml中更改此内容:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/checkbox_off_background"
android:layout_marginLeft="62dp"
android:layout_marginStart="62dp"
android:layout_marginTop="15dp"
android:id="@+id/imgB1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="Clicked" /> //here changes are made