如何使两个单独的按钮在一个活动中做两种不同的事情? [英] How can I make two separate buttons do two different things in an activity?

查看:152
本文介绍了如何使两个单独的按钮在一个活动中做两种不同的事情?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使一个按钮计算并显示货币(该按钮正在工作),而我试图使另一个按钮打开另一个活动并在此处显示货币计算.

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.

我尝试了不同的方法来尝试为btnSubmit(显示货币计算的结果)和btnDifferent(在显示货币计算结果的单独活动中打开结果)提供两个不同的选项.

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 + "");

        }
    }

推荐答案

使用setOnClickListener方法似乎更优雅.

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

这篇关于如何使两个单独的按钮在一个活动中做两种不同的事情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆