0

MainActivity画面に2つのボタンがあります。Button1はユーザー入力を処理し、Button2はユーザーを次のアクティビティ画面に導きます。この画面には、最初のボタンで処理された出力に基づいて別の出力が表示されます。Button1をクリックするたびに入力が計算されますが、自動的に次のアクティビティ画面に移動し、必要な出力が表示されるため、Button2が使用できなくなります。Button2をクリックすると、次のアクティビティ画面が表示されますが、必要な出力が表示されず、テキストビューのIDのみが表示されます。

両方のボタンのonClick属性はcalculateClickHandlerです。問題は、Button2の属性がButton1とは異なっている必要があることだと思います。Button2用に別のクリックハンドラーを作成しようとしましたが、機能しませんでした。

public void nextClickHandler(View view)
    {
        if (view.getId() == R.id.DGButton)
        {
            Intent myIntent = new Intent(MainActivity.this, Activity2.class);
            startActivity(myIntent);
        }
    }

これは、MainActivityの計算ボタンクリックハンドラーです。

public void calculateClickHandler(View view)
        {
            //handle the click of the calculator button

            if (view.getId() == R.id.CalculateButton)
            {
                //code here

                Button next = (Button) findViewById(R.id.DGButton);
                next.setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View view)
                    {
                        Intent myIntent = new Intent(MainActivity.this, Activity2.class);
                        startActivity(myIntent);
                    }
                });

                Intent intent1 = new Intent(MainActivity.this, Activity2.class);
                intent1.putExtra("key", bmiValue);
                startActivity(intent1);  
            }   
        }

Activity2の[戻る]ボタンクリックハンドラー:

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Button next = (Button) findViewById(R.id.BackToBMI);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        TextView dietplanText = (TextView)findViewById(R.id.DietPlanText);
        TextView categoryText = (TextView)findViewById(R.id.BMICategory);

        Bundle extras = getIntent().getExtras();
        if(extras != null)
        {
                //code here
            }
    }

私はAndroidプログラミングの初心者なので、どんな助けでも大歓迎です。

4

1 に答える 1

1

calculateClickHandler(View view) では、if 条件の後に else を使用してアクティビティを開始する必要があります。

「両方のボタンの onClick 属性は calculateClickHandler です。」- あなたは言いました。しかし、nextClickHandler(View view) とは何なのか、理解できません。

于 2013-01-05T19:11:25.300 に答える