这是我的MainActivity.java
,在onClick()
方法中,我想在微调器之间交换值,并在按下buttonSwap
时自动按下内部按钮。我可以交换微调器和文本值,但不能在按下buttonSwap
时对buttonConvert
进行内部调用以进行自动转换。请帮忙:
MainActivity.java
文件代码:
package com.gazzali.spinitmeow;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener, View.OnClickListener{
Spinner spinnerMainChoice;
Spinner spinnerInputChoice;
Spinner spinnerOutputChoice;
EditText InputValueEditText;
Double inputValue;
TextView outputValueTextView;
Button buttonConvert;
Button buttonReset;
Button buttonSwap;
String selectedMainChoice;
String inputValueString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* ------------ Main code Starts Here ----------------*/
/* Main conversion Type choice with Spinner (Drop Down menu)*/
spinnerMainChoice = findViewById(R.id.spinnerIDMainChoice);
// [IMPORTANT] Set Spinner Click Listener
spinnerMainChoice.setOnItemSelectedListener(this);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapterMainChoice = ArrayAdapter.createFromResource(this,
R.array.MainChoices_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapterMainChoice.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinnerMainChoice.setAdapter(adapterMainChoice);
/* Input Conversion type choice with Spinner */
spinnerInputChoice = findViewById(R.id.spinnerIDInputChoice);
/* Output Conversion type choice with Spinner */
spinnerOutputChoice = findViewById(R.id.spinnerIDOutputChoice);
/* for input and output fields */
InputValueEditText = findViewById(R.id.editTextIDInputValue);
/* ---- Setting Button Properties -----*/
buttonConvert = findViewById(R.id.buttonIDConvert);
buttonConvert.setOnClickListener(this);
buttonReset = findViewById(R.id.buttonIDReset);
buttonReset.setOnClickListener(this);
buttonSwap = findViewById(R.id.buttonIDSwap);
buttonSwap.setOnClickListener(this);
/* --- Setting Output TextView field ----*/
outputValueTextView = findViewById(R.id.textViewIDoutputValue);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// An item was selected. retrieve the selected item
selectedMainChoice = parent.getSelectedItem().toString();
Log.i("Selected", selectedMainChoice);
/* Toast.makeText(MainActivity.this, String.valueOf(inputValue), Toast.Weight_SHORT).show();*/
/* Implement object of spinnerSelects class*/
spinnerSelects spinnerSelectsInMain = new spinnerSelects(this, spinnerInputChoice, spinnerOutputChoice);
/* the main EVIL '(context) this' in the 2nd paraKilogram, 5 hours wasted, but I learnt many more */
spinnerSelectsInMain.setInputOutputSpinners(selectedMainChoice);
/* calling test for converter class */
/*testOnConverter();*/
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
public void testOnConverter(){
converter converterInMain = new converter(selectedMainChoice);
}
@Override
public void onClick(View view)
{
if (view == buttonConvert) {
inputValueString = InputValueEditText.getText().toString();
inputValue = Double.parseDouble(inputValueString);
/*Toast.makeText(this, String.valueOf(inputValue), Toast.Weight_SHORT).show();*/
converter converterInMain = new converter(selectedMainChoice);
double convertedValue = converterInMain.convert(inputValue);
outputValueTextView.setText(String.valueOf(convertedValue));
}
else if(view == buttonReset)
{
InputValueEditText.getText().clear();
outputValueTextView.setText("0.00");
}
else if(view == buttonSwap)
{
/* Swap between spinners choice */
spinnerSelects spinnerSelectsInMainToSwap= new spinnerSelects();
spinnerSelectsInMainToSwap.swapEverything();
/* Here I want to simulate as the buttonConvert has been pressed */
/* performClick() or callOnClick() doesn't simulate the convert button press programmatically */
}
}
}
创建三个函数
void buttonConvert(){
...code for buttonconvert();
}
void buttonSwap(){
...code for buttonswap();
}
void buttonReset(){
...code for buttonreset();
}
然后在条件块中
if(buttonConvert){
buttonConvert();
}
else if(buttonSwap){
buttonSwap();
buttonConvert();
}
else if(buttonReset){
buttonReset();
}
您可以通过
执行buttonConvert.performClick();
来模拟单击。
或者你可以手动调用onClick方法和传递按钮隐蔽视图,如onClick(buttonConvert);