How can I make two separate buttons do two different things in an activity?

对着背影说爱祢 提交于 2020-01-25 03:57:11

问题


I'm trying to make one button calculate and display currency (this one is working) and I'm trying to make the other one open another activity and display the currency calculation there.

I've tried different ways of trying to have two different options for btnSubmit (which shows the result of the currency calculation) and btnDifferent (which opens the result in the separate activity displaying the currency calculation result).

Now I can only manage to calculate the result and open the other activity at the same time.

public class MainActivity extends AppCompatActivity {

    public Spinner spnCurrency1, spnCurrency2;
    public Button btnSubmit;
    public Button btnDifferent;
    public EditText from;
    public TextView to;

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

        btnSubmit = (Button) findViewById(R.id.btnSubmit);
        btnDifferent = (Button) findViewById(R.id.btnDifferent);
        from = (EditText) findViewById(R.id.InputEditText);
        to = (TextView) findViewById(R.id.OutputTextView);


        spnCurrency1 = (Spinner) findViewById(R.id.spnCurrency1);
        List<String> lstCurrency1 = new ArrayList<String>();
        lstCurrency1.add("Euro");
        lstCurrency1.add("USD");
        lstCurrency1.add("Pound");
        ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, lstCurrency1);
        dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spnCurrency1.setAdapter(dataAdapter1);


        spnCurrency2 = (Spinner) findViewById(R.id.spnCurrency2);
        List<String> lstCurrency2 = new ArrayList<String>();
        lstCurrency2.add("Euro");
        lstCurrency2.add("USD");
        lstCurrency2.add("Pound");
        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, lstCurrency2);
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spnCurrency2.setAdapter(dataAdapter2);
    }

    public void onClick(View v) {

        int index1 = spnCurrency1.getSelectedItemPosition();
        int index2 = spnCurrency2.getSelectedItemPosition();
        float value = Float.parseFloat(from.getText().toString());



               Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);


                float ratio[] = {0.9f, 1.0f, 0.78f};
                float result = value / ratio[index1] * ratio[index2];
                to.setText(result + "");

        }
    }

回答1:


Check with ID of the view and do action based on that:

public void onClick(View v) {
    int index1 = spnCurrency1.getSelectedItemPosition();
    int index2 = spnCurrency2.getSelectedItemPosition();
    float value = Float.parseFloat(from.getText().toString());

    float ratio[] = {0.9f, 1.0f, 0.78f};
    float result = value / ratio[index1] * ratio[index2];

    switch (v.getId()) {
        case R.id.btnSubmit:
            to.setText(result + "");
            break;
        case R.id.btnDifferent:
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            intent.putExtra("Result", result);
            startActivity(intent);
            break;
    }
}



回答2:


Using setOnClickListener method is seems more elegant.

    btnSubmit = (Button) findViewById(R.id.btnSubmit);
    btnDifferent = (Button) findViewById(R.id.btnDifferent);

    btnSubmit.setOnClickListener(new OnClickListener(){
      public void onClick(View view) {
            //do submit
      }
    });
    btnDifferent.setOnClickListener(new OnClickListener(){
      public void onClick(View view) {
            //do different
      }
    });


来源:https://stackoverflow.com/questions/58527327/how-can-i-make-two-separate-buttons-do-two-different-things-in-an-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!