実現したいこと
activity_main.xmlファイル上の「設定」ボタンを押したとき、users_information.xmlファイルに移動したい。
発生している問題・分からないこと
activity_main.xmlファイルとusers_information.xmlファイルの二つのファイルは出来ていますが、それに適応している2つのjavaファイルのプログラムをどうしたら良いのかがわからない。
該当のソースコード
activity_main.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:id="@+id/main" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".MainActivity"> 9 10 <Button 11 android:id="@+id/informationBtn1" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:layout_margin="16dp" 15 android:text="@string/informationBtn1" 16 android:textSize="25sp" 17 android:textColor="@color/black" 18 style="@style/Widget.Material3.Button.TextButton.Icon" 19 app:layout_constraintStart_toStartOf="parent" 20 app:layout_constraintTop_toTopOf="parent" /> 21 22 <TextView 23 android:id="@+id/name" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:layout_margin="25dp" 27 android:text="@string/name_label" 28 android:textIsSelectable="false" 29 android:textSize="30sp" 30 android:textColor="@color/my_custom_color" 31 android:textStyle="bold" 32 app:layout_constraintEnd_toEndOf="parent" 33 app:layout_constraintTop_toTopOf="parent" /> 34 35 <Button 36 android:id="@+id/tellBtn2" 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:layout_marginTop="-55dp" 40 android:text="@string/telephoneBtn2" 41 android:textSize="56sp" 42 android:textStyle="bold" 43 app:layout_constraintBottom_toBottomOf="parent" 44 app:layout_constraintEnd_toEndOf="parent" 45 app:layout_constraintStart_toStartOf="parent" 46 app:layout_constraintTop_toBottomOf="@id/name" /> 47 48</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
java
1 2import android.content.Intent; 3import android.os.Bundle; 4import android.view.View; 5import android.widget.Button; 6import android.widget.TextView; 7 8import androidx.activity.EdgeToEdge; 9import androidx.appcompat.app.AppCompatActivity; 10import androidx.core.graphics.Insets; 11import androidx.core.view.ViewCompat; 12import androidx.core.view.WindowInsetsCompat; 13 14public class MainActivity extends AppCompatActivity implements View.OnClickListener { 15 16 private Button informationBtn1, tellBtn2; 17 private TextView name; 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 EdgeToEdge.enable(this); 23 setContentView(R.layout.activity_main); 24 ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { 25 Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); 26 v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); 27 return insets; 28 }); 29 30 // idからviewを見つける 31 Button information_btn1 = findViewById(R.id.informationBtn1); 32 String inform = "設定"; 33 information_btn1.setText(inform); 34 information_btn1.setOnClickListener(v -> { 35 Intent intent = new Intent(MainActivity.this, UsersInformation.class); 36 startActivity(intent); 37 }); 38 39 TextView name = findViewById(R.id.name); 40 String label = "名前"; 41 name.setText(label); 42 43 Button tell_btn2 = findViewById(R.id.tellBtn2); 44 String call = "○○"; 45 tell_btn2.setText(call); 46 } 47 48 @Override 49 public void onClick(View v) { 50 // ここには他のクリックイベントの処理を書くことができます 51 } 52} 53
users_information.xml
xml
1 2<?xml version="1.0" encoding="utf-8"?> 3<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 android:id="@+id/sub" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".MainActivity"> 9 10 <LinearLayout 11 android:id="@+id/content_layout" 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" 14 android:layout_above="@+id/btn_withdraw" 15 android:layout_marginBottom="-1dp" 16 android:orientation="vertical" 17 android:padding="16dp"> 18 19 <!-- ご使用者様 --> 20 <TextView 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:paddingTop="3dp" 24 android:paddingBottom="1dp" 25 android:text="@string/use_label" 26 android:textSize="18sp" /> 27 28 <!-- ご使用者様の入力欄 --> 29 <EditText 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" 32 android:background="@android:drawable/edit_text" 33 android:hint="名前を持ってくる"/> 34 35 <!-- 電話番号 --> 36 <TextView 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:paddingTop="3dp" 40 android:paddingBottom="1dp" 41 android:text="@string/phoneNo_label" 42 android:textSize="18sp" /> 43 44 <EditText 45 android:layout_width="match_parent" 46 android:layout_height="wrap_content" 47 android:background="@android:drawable/edit_text" 48 android:hint="電話番号を入力" 49 android:inputType="phone" /> 50 51 <!-- メールアドレス --> 52 <TextView 53 android:layout_width="wrap_content" 54 android:layout_height="wrap_content" 55 android:paddingTop="3dp" 56 android:paddingBottom="1dp" 57 android:text="@string/email_label" 58 android:textSize="18sp" /> 59 60 <EditText 61 android:layout_width="match_parent" 62 android:layout_height="wrap_content" 63 android:background="@android:drawable/edit_text" 64 android:hint="メールアドレスを入力" 65 android:inputType="textEmailAddress" /> 66 67 <!-- ご登録者様メールアドレス --> 68 <TextView 69 android:layout_width="wrap_content" 70 android:layout_height="wrap_content" 71 android:paddingTop="3dp" 72 android:paddingBottom="1dp" 73 android:text="@string/entry_label" 74 android:textSize="18sp" /> 75 76 <EditText 77 android:layout_width="match_parent" 78 android:layout_height="wrap_content" 79 android:background="@android:drawable/edit_text" 80 android:hint="@string/email_hint" 81 android:inputType="textEmailAddress" /> 82 83 <!-- ボタンを含むレイアウト --> 84 <LinearLayout 85 android:layout_width="match_parent" 86 android:layout_height="wrap_content" 87 android:gravity="center" 88 android:orientation="horizontal" 89 android:paddingTop="16dp"> 90 91 <Button 92 android:id="@+id/btn_back" 93 android:layout_width="wrap_content" 94 android:layout_height="wrap_content" 95 android:text="@string/back_label" /> 96 97 <Button 98 android:id="@+id/btn_confirm" 99 android:layout_width="wrap_content" 100 android:layout_height="wrap_content" 101 android:layout_marginStart="16dp" 102 android:text="@string/decide_label" /> 103 </LinearLayout> 104 105 </LinearLayout> 106 107 <Button 108 android:id="@+id/btn_withdraw" 109 android:layout_width="wrap_content" 110 android:layout_height="wrap_content" 111 android:text="@string/leave_label" 112 android:layout_alignParentBottom="true" 113 android:layout_alignParentEnd="true" /> 114 115</RelativeLayout> 116
UsersInformation.java
java
1 2import android.os.Bundle; 3 4import androidx.activity.EdgeToEdge; 5import androidx.appcompat.app.AppCompatActivity; 6import androidx.core.graphics.Insets; 7import androidx.core.view.ViewCompat; 8import androidx.core.view.WindowInsetsCompat; 9 10public class UsersInformation extends AppCompatActivity { 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 EdgeToEdge.enable(this); 16 setContentView(R.layout.users_information); 17 ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.sub), (v, insets) -> { 18 Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); 19 v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); 20 return insets; 21 }); 22 } 23}
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
ChatGPT等にも聞いたが、現状「設定」ボタンを押すとAndroidのホーム画面に戻ってしまう。
補足
特になし

回答1件
あなたの回答
tips
プレビュー