HOME技術AndroidAndroidアプリ:画面遷移時に値を渡す

Androidアプリ:画面遷移時に値を渡す

Androidで画面遷移時に値を渡す方法です。

数値を渡す場合

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener(){
           @Override
            public void onClick(View v){
               Intent intent = new Intent(getApplication(),QuestionActivity.class);
               intent.putExtra("question",1);
               startActivity(intent);
           }
        });
    }
}

activity_main.xml

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="ボタン"
    android:textSize="30sp"
    />

SubActivity.java

public class SubActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub);

        TextView text = (TextView)findViewById(R.id.text);
        Intent intent = getIntent();
        Integer data = intent.getIntExtra("question",0);
        text.setText(String.valueOf(data));

        Button backButton = findViewById(R.id.back);
        backButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                finish();
            }
        });
    }
}

activity_sub.xml

<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="30dp"/>

<Button
    android:id="@+id/back"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:text="back"
    />

文字列を渡す場合

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener(){
           @Override
            public void onClick(View v){
               Intent intent = new Intent(getApplication(),QuestionActivity.class);
               intent.putExtra("question","文字列");
               startActivity(intent);
           }
        });
    }
}

activity_main.xml

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="ボタン"
    android:textSize="30sp"
    />

SubActivity.java

public class SubActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub);

        TextView text = (TextView)findViewById(R.id.text);
        Intent intent = getIntent();
        String data = intent.getStringExtra("question");
        text.setText(data);

        Button backButton = findViewById(R.id.back);
        backButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                finish();
            }
        });
    }
}

activity_sub.xml

<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="30dp"/>

<Button
    android:id="@+id/back"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:text="back"
    />

関連記事

Androidアプリ:ボタンのテキストを変える

Androidでボタンのテキストをプログラム上で変える方法です。 MainActivity.java   Button button = findViewById(R.id.button);…続きを読む

Androidアプリ:ボタンの仕様を1つのxmlファイルにまとめる

Android Studioで、ボタンなどの色や形の指定をする際、1つのxmlファイルで、ボタンを押したときと押していないときの状態を指定する方法です。 xmlファイルの指定 Android Stud…続きを読む

Androidアプリ:ボタンやEditTextに背景色・枠線をつける

Android Studioで、ボタンやEditTextに背景色や枠線をつける方法です。 コントロールの見た目の設定をするには、別途専用のxmlファイルを生成して、そこに書いた設定を読み込むようにしま…続きを読む

Androidアプリ:App is not indexable by Google Search;と表示される

Android Studioで、 App is not indexable by Google Search; consider adding at least one Activity with a…続きを読む

Androidアプリ:背景に画像を指定する

Android Studioを使って、背景に画像を設定する方法です。 背景画像を格納する 今回は以下の画像を使います。 画像サイズなどは適当につくります。 Androidは様々な解像度がありますが、画…続きを読む

Androidアプリ:タイトルバーを消す

Android Studioで、タイトルバーを消す方法です。 通常は、以下のように画面上部にタイトルバーが表示されています。 AndroidManifest.xmlを編集する android:them…続きを読む

Androidアプリ:When using intent filters, please specify ‘android:exported’ as well

Android Studioで「When using intent filters, please specify ‘android:exported’ as well」という…続きを読む

Google広告でポリシー違反「句読点と記号」に対応する

モバイルアプリのGoogle広告のキャンペーンを設定しましたが、「ポリシー違反1件」と表示されました。 ポリシー違反「句読点と記号」 ポリシー違反の詳細を見ると、「句読点と記号」というヒントが表示され…続きを読む

Androidアプリ:画面の高さと幅を取得する

Andoroidで、画面の高さと幅を取得する方法です。 MainActivity.java Android端末の画面サイズ(高さと幅)を表示します。 public class QuestionActi…続きを読む

Androidアプリ:ライブラリの更新とエラーの修正

Androidアプリを開発していて、Build.gradle(app)の、dependenciesの項目内でハイライト表示されたり、赤い下線が引かれたりすることがあります。 ハイライトはライブラリを更…続きを読む