[英]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.