LoginSignup
0
0

More than 1 year has passed since last update.

AndroidStudio ボタンで画面遷移

Posted at

初学者です。Android studioでボタンを押すと次の画面へ切り替わる方法を学習しました。

◇xml

xmlにButtonをいれ、onClickにメソッド名を決めます。

 <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_next"
        android:layout_marginTop="80dp"
        android:onClick="toNext"/>

◇Activity

Activityはintentで画面遷移をします。xmlのonClickで決めたメソッドの中にintentを入れます。

public void toNext(View view) { 
Intent intent = new Intent({今の画面Activity名}.this, {次の画面Activity名}.class);
        startActivity(intent);
}

◇AndroidManifest

1番目の画面がtrue、 2番目の画面以降はfalseにします。

        <activity
            android:name=".{2番目以降の画面Activity名}"
            android:exported="false" />
        <activity
            android:name=".{最初の画面Activity名}"
            android:exported="true" >

◇動きました!

2023-03-30-17-05-28.gif

参考にさせていただきました:
https://codeforfun.jp/android-studio-how-to-set-button-click-event-with-java/
https://zenn.dev/yass97/articles/26e3f92a0d7d0d
https://akira-watson.com/android/activity-1.html
https://codeforfun.jp/android-studio-quiz-game-7/

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0