简体   繁体   中英

Click Handler of Button in Android Eclipse

I have 2 buttons on the MainActivity screen. The Button1 processes the user inputs while the Button2 leads the user to the next activity screen which shows another output based on the processed output by the first button. Whenever I click Button1, it calculates the inputs but it automatically leads me to the next activity screen and shows me the output I need which makes Button2 become unusable. When I click Button2, it leads me to the next activity screen but it won't show the output i need, it only shows the ids of the textviews.

The onClick attribute of both buttons is calculateClickHandler. I think the problem is that the attribute of Button2 must be different from Button1. I tried making another click handler for Button2, but it did not work:

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

This is MainActivity's Calculate Button click handler:

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's Back button click handler:

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
            }
    }

I'm a beginner in Android programming so any help would be much appreciated.

In calculateClickHandler(View view) you should use else after if condition to start an activity.

"The onClick attribute of both buttons is calculateClickHandler."- you said. but then what is nextClickHandler(View view) for I cant understand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM