Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使按钮在单击时重新出现在不同的位置?_Java_Android_Android Layout_Button - Fatal编程技术网

Java 如何使按钮在单击时重新出现在不同的位置?

Java 如何使按钮在单击时重新出现在不同的位置?,java,android,android-layout,button,Java,Android,Android Layout,Button,几周前,我开始在Android studio学习编码。现在我正在尝试制作一个有9个按钮的应用程序,但是你总是只能看到其中的一个。当你们点击“可见”按钮时,它应该消失并显示另一个按钮,以此类推。 这就是我到目前为止所做的: MainActivity.java 在每次单击按钮属性时,我都会设置一个函数ButtonDispear。在我看来一切正常,没有错误,但当我运行程序并单击第一个按钮b1时,应用程序就停止了。你能告诉我为什么会发生这种情况,我怎样才能解决它?多谢各位 更新 activity_mai

几周前,我开始在Android studio学习编码。现在我正在尝试制作一个有9个按钮的应用程序,但是你总是只能看到其中的一个。当你们点击“可见”按钮时,它应该消失并显示另一个按钮,以此类推。 这就是我到目前为止所做的:

MainActivity.java

在每次单击按钮属性时,我都会设置一个函数ButtonDispear。在我看来一切正常,没有错误,但当我运行程序并单击第一个按钮b1时,应用程序就停止了。你能告诉我为什么会发生这种情况,我怎样才能解决它?多谢各位

更新

activity_main.xml

你说你只有9个按钮,名字从b1-b9开始 当您生成一个始终大于或等于10的数字时

public void buttonDisappear(Button view){
    shownButton.setVisibility(View.INVISIBLE);
    Random generatedNumber = new Random();
    int buttonNumber = generatedNumber.nextInt(9)+1;
    String nextShownButton = "b"+String.valueOf(buttonNumber);
    int nextShownButtonId = getResources().getIdentifier(nextShownButton, "id", getPackageName());

    Button shownButton = (Button) findViewById(nextShownButtonId);
    shownButton.setVisibility(View.VISIBLE);

}

将您的ButtonDiskBearButton视图更改为ButtonDiskBearView视图请发布StackTrace请共享xml和stacktraceTo Jerrol oh。。。我错了,但这改变不了什么。给John Joe我在哪里能找到stacktrace?你好,我是弗罗加托:是的,从b1到b9。给戈德温:等一下,我来编辑我的帖子。接得好!顺便说一下,仅仅为了设置按钮的可见性,不需要将findViewById的返回值强制转换为Button。FindViewByDNExtShownButtonid.setVisibilityView.VISIBLE就足够了。@HiI'mFrogatto,这是正确的,但是casting不会让应用停止是的,你的绝对正确。但这不是必须的。@Godwin谢谢你的纠正:刚才我注意到我的另一个错误Button shownButton=Button findViewBydNextshownButtonid;应更改为just shownButton=Button findViewByDNextshownbuttonId;。而且效果很好。我不应该创建两个不同的按钮变量,只使用我已经创建的一个。谢谢大家的帮助^-^
  <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:onClick="buttonDisappear"
        android:text="@string/mygtukas"
        android:visibility="invisible"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="48dp"
        android:onClick="buttonDisappear"
        android:text="@string/mygtukas"
        android:visibility="invisible"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/b9" />

    <Button
        android:id="@+id/b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:layout_marginTop="48dp"
        android:onClick="buttonDisappear"
        android:text="@string/mygtukas"
        android:visibility="invisible"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/b5" />

    <Button
        android:id="@+id/b4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="48dp"
        android:onClick="buttonDisappear"
        android:text="@string/mygtukas"
        android:visibility="invisible"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/b5" />

    <Button
        android:id="@+id/b5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="48dp"
        android:onClick="buttonDisappear"
        android:text="@string/mygtukas"
        android:visibility="invisible"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/b7" />

    <Button
        android:id="@+id/b6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:layout_marginTop="48dp"
        android:onClick="buttonDisappear"
        android:text="@string/mygtukas"
        android:visibility="invisible"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/b2" />

    <Button
        android:id="@+id/b7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="48dp"
        android:onClick="buttonDisappear"
        android:text="@string/mygtukas"
        android:visibility="invisible"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/b2" />

    <Button
        android:id="@+id/b9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:onClick="buttonDisappear"
        android:text="@string/mygtukas"
        android:visibility="invisible"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
public void buttonDisappear(Button view){
    shownButton.setVisibility(View.INVISIBLE);
    Random generatedNumber = new Random();
    int buttonNumber = generatedNumber.nextInt(9)+1;
    String nextShownButton = "b"+String.valueOf(buttonNumber);
    int nextShownButtonId = getResources().getIdentifier(nextShownButton, "id", getPackageName());

    Button shownButton = (Button) findViewById(nextShownButtonId);
    shownButton.setVisibility(View.VISIBLE);

}