简体   繁体   English

我怎样才能让两个单独的按钮在一个活动中做两个不同的事情?

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

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

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

        }
    }

Check with ID of the view and do action based on that:检查视图的 ID 并根据它执行操作:

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

Using setOnClickListener method is seems more elegant.使用 setOnClickListener 方法似乎更优雅。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使片段中的两个以上按钮打开不同的活动 - How To Make More Than Two buttons in Fragment to Open Different Activity 单击两个不同的按钮时,如何使用不同的文本启动相同的 Activity? - How do I start same Activity with different text when click two different buttons? 我们如何使一个活动从两个不同的活动中接收两个可包裹的物体 - How can we make an activity receive two parcelable objects from two different activities 如何从两个具有不同ui的单独图标中打开主要活动 - How to open main activity from two separate icons with different ui 如何将来自单独的AsyncTask内部类的两个结果传递给主活动中的相同方法? - How do I pass two results from separate AsyncTask inner classes to the same method in the main activity? 在同一活动中单击两个不同的按钮时,如何导航到两个不同的活动? - How to navigate to two different activities on clicking two different buttons in same activity? 如何在Android的一项活动中从两个sqlite表中获取两个列表视图 - how can I make two listviews fetching from two sqlite tables in one activity in Android 如何使TextView中的文本有两种不同的大小? - How can I make the text in TextView be two different sizes? 如何在setOnDragListener中使用两个按钮? - How can i use two buttons in setOnDragListener? 如何通过两个Activity实现侦听器? - How can I implements listener by two Activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM