LoginSignup
2
2

More than 5 years have passed since last update.

画面遷移 情報の受け渡し 備忘録

Last updated at Posted at 2017-06-10

画面遷移 情報の受け渡し

MainActivity

時刻・コメント取得は前回の物を流用

IntentでSubActivtyへ値を受け渡す
intent.putExtra("String Value", comment);
intent.putExtra("int Value", quantity);

        intent.putExtra("String Value", comment);
        intent.putExtra("int Value", quantity);
    private void addQuantityInfo() {
        // 数量の変数宣言
        private int quantity = 0;
        // コメントを取得する
        EditText edittext = (EditText) findViewById(R.id.edit_comment);
        String comment = edittext.getText().toString();
        // 画面遷移
        Intent intent = new Intent(getApplication(), DetailScreen.class);
        intent.putExtra("String Value", comment);
        intent.putExtra("int Value", quantity);
        startActivity(intent);
    }

SubActivty

CharSequence=文字列を読み取っている
CharSequence getstring これで取得した文字列を読み取っている

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_screen);
        setTitle("");

        Intent intent = getIntent();
        //MainActivityから値を受け取る
        CharSequence getstring = intent.getCharSequenceExtra("String Value");
        int getint = intent.getIntExtra("int Value",0);


        //id textView1をt1に当てはめている
        TextView t1 = (TextView)findViewById(R.id.textView1);
        //id textView1をt2に当てはめている
        TextView t2 = (TextView)findViewById(R.id.textView2);

        //受け取った値を表示
        t1.setText("受け取った文字は"+getstring);
        t3.setText("受け取った数値は"+String.valueOf(getint));
2
2
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
2
2