はじめに
Activityの切り替えで画面遷移して値も渡してみました。
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
java
package com.example.testapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView txt;
private TextView nextTxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView)this.findViewById(R.id.text1);
}
//ボタン(こんにちは)
public void jHelloButton(View view){
txt.setText(R.string.temp1);
}
//ボタン(HELLO)
public void helloButton(View view){
txt.setText(R.string.temp2);
}
//画面遷移ボタン
public void nextButton(View view){
setContentView(R.layout.next);
nextTxt = (TextView)this.findViewById(R.id.next_text1);
nextTxt.setText(txt.getText());
}
//画面遷移(Activity切り替え)
public void nextActivityButton(View view){
Intent intent = new Intent(this,SubActivity.class);
intent.putExtra("message", txt.getText().toString());
startActivity(intent);
}
}
nextActivityButton()
でActivityの切り替えを行っています。
Intent intent = new Intent(this,SubActivity.class);
で現在のクラスと遷移先のActivityクラスを指定しています。
intent.putExtra("message", txt.getText().toString());
で「message」というキーで値を設定しています。
startActivity(intent);
でActivityを切り替えます。
package com.example.testapp;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SubActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 画面レイアウトをセット
setContentView(R.layout.activity_sub);
// レイアウト内のテキストフィールドのプロパティを取得
TextView txt = (TextView)this.findViewById(R.id.activity_sub_text);
// 遷移元で渡した値を取得(キーバリューの形式で渡されている)
Intent intent = getIntent();
// キーを指定して値を取り出す
String mes = intent.getStringExtra("message");
txt.setText(mes);
}
}
setContentView(R.layout.activity_sub);
でActivityで使用するレイアウトを指定しています。
Intent intent = getIntent();
で遷移元で生成したインスタンスを取得しています。
String mes = intent.getStringExtra("message");
で遷移元で設定したキーが「message」の値を取り出しています。
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/activity_sub_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
ヘッダーのタイトル部分を定義しているようです。
android:label="@string/app_name"
で参照元を指定しています。(参照元にはxmlがあります。)
manifests
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TestApp">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="SubActivity"/>
</application>
</manifest>
おわりに
AndroidManifest.xmlで<activity android:name="SubActivity"/>
を設定しないとエラーになります。(ハマりました)