-3

私は次のものを持っています:

</LinearLayout>
                <LinearLayout
        android:id="@+id/layout8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button 
            android:layout_height="wrap_content"
            android:layout_width="110dp"
            android:layout_weight="1"
            android:text="@string/button1"/>
          <Button 
            android:layout_height="wrap_content"
            android:layout_width="110dp"
            android:layout_weight="1"
            android:text="@string/button2"
            android:onClick="clickButton"/>

アクティビティ1:

            public void clickButton(View v) {
                Button btn = (Button)v;
                String buttonText = btn.getText().toString();
                Intent intent = new Intent(MainActivity.this, PickChar2.class);
                intent.putExtra("button2", buttonText);
                startActivity(intent);

            }

アクティビティ 2:

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String foo = extras.getString("button1"); // how do i accept any button?

        TextView textView = (TextView) findViewById(R.id.header);

最初に getString が任意のボタンを受け入れる方法はありますか?次に if ステートメントを実行して、ユーザーがクリックしたボタンを確認し、それに応じて行動しますか? ユーザーがボタン1、ボタン2、または3、4などを押すことができるようにしたい...

私が抱えている問題は、最初のレイアウトに 30 個のボタンがあり、30 個のインテントと 30 個の extra.getStrings を作成したくないということです。私がやりたいことは次のようなものです: -user 1 press1 button - アクティビティ 1 はそのボタンをアクティビティ 2 に渡します - アクティビティ 2 はどのボタンが押されたかをチェックします - 何かをします

ボタンは別のアクティビティにあります。ボタンを activity1 からこのアクティビティに渡すインテントを使用しました。

4

1 に答える 1