我正在制作一个简单的计算器但是使用微调器作为选择操作员的方式,所以每当我按下"计算"它从不计算它但是当我更改在微调器上选择的项目时,它会用它们各自的运算符计算x和y。如何在onClickListener中正确使用/编码onItemSelectedListener?
这是我的代码。
package com.example.leks.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
private TextView tvResult;
private EditText etFirstnumber, etSecondnumber;
private Button btnCalculate;
private Spinner spnOperators;
private int result;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init()
{
etFirstnumber = (EditText) findViewById(R.id.etFirstnumber);
etSecondnumber = (EditText) findViewById(R.id.etSecondnumber);
spnOperators = (Spinner) findViewById(R.id.spnOperators);
tvResult = (TextView) findViewById(R.id.tvResult);
btnCalculate = (Button) findViewById(R.id.btnCalculate);
btnCalculate.setOnClickListener(this);
}
@Override
public void onClick(View view)
{
try {
switch (view.getId()) {
case R.id.btnCalculate:
spnOperators.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long l)
{
String num1 = etFirstnumber.getText().toString();
String num2 = etSecondnumber.getText().toString();
int x = Integer.parseInt(num1);
int y = Integer.parseInt(num2);
String SelectedItem = parent.getItemAtPosition(position).toString();
if (SelectedItem.equals("+"))
{
result = x + y;
tvResult.setText(String.valueOf(result));
}
if (SelectedItem.equals("-"))
{
result = x - y;
tvResult.setText(String.valueOf(result));
}
if (SelectedItem.equals("*"))
{
result = x * y;
tvResult.setText(String.valueOf(result));
}
if (SelectedItem.equals("/"))
{
result = x/y;
tvResult.setText(String.valueOf(result));
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView)
{
}
});
break;
}
}catch(Exception e)
{
tvResult.setText("Error");
}
}
}
答案 0 :(得分:0)
请将你的onItemSelectedListener放在init方法中。
您不需要将它放在onClick方法上。
private void init()
{
etFirstnumber = (EditText) findViewById(R.id.etFirstnumber);
etSecondnumber = (EditText) findViewById(R.id.etSecondnumber);
spnOperators = (Spinner) findViewById(R.id.spnOperators);
tvResult = (TextView) findViewById(R.id.tvResult);
btnCalculate = (Button) findViewById(R.id.btnCalculate);
btnCalculate.setOnClickListener(this);
spnOperators.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long l)
{
String num1 = etFirstnumber.getText().toString();
String num2 = etSecondnumber.getText().toString();
int x = Integer.parseInt(num1);
int y = Integer.parseInt(num2);
String SelectedItem = parent.getItemAtPosition(position).toString();
if (SelectedItem.equals("+"))
{
result = x + y;
tvResult.setText(String.valueOf(result));
}
if (SelectedItem.equals("-"))
{
result = x - y;
tvResult.setText(String.valueOf(result));
}
if (SelectedItem.equals("*"))
{
result = x * y;
tvResult.setText(String.valueOf(result));
}
if (SelectedItem.equals("/"))
{
result = x/y;
tvResult.setText(String.valueOf(result));
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView)
{
}
});
}
答案 1 :(得分:0)
尝试以下代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
String num1="",num2="";
int x=0,y=0;
String SelectedItem ;
private TextView tvResult;
private EditText etFirstnumber, etSecondnumber;
private Button btnCalculate;
private Spinner spnOperators;
private int result;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init()
{
etFirstnumber = (EditText) findViewById(R.id.etFirstnumber);
etSecondnumber = (EditText) findViewById(R.id.etSecondnumber);
spnOperators = (Spinner) findViewById(R.id.spnOperators);
tvResult = (TextView) findViewById(R.id.tvResult);
btnCalculate = (Button) findViewById(R.id.btnCalculate);
btnCalculate.setOnClickListener(this);
num1 = etFirstnumber.getText().toString();
num2 = etSecondnumber.getText().toString();
x = Integer.parseInt(num1);
y = Integer.parseInt(num2);
spnOperators.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long l)
{
SelectedItem = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView)
{
}
});
}
@Override
public void onClick(View view)
{
try {
switch (view.getId()) {
case R.id.btnCalculate:
calculate();
break;
}
}catch(Exception e)
{
tvResult.setText("Error");
}
}
public void calculate(){
if (SelectedItem.equals("+"))
{
result = x + y;
tvResult.setText(String.valueOf(result));
}
if (SelectedItem.equals("-"))
{
result = x - y;
tvResult.setText(String.valueOf(result));
}
if (SelectedItem.equals("*"))
{
result = x * y;
tvResult.setText(String.valueOf(result));
}
if (SelectedItem.equals("/"))
{
result = x/y;
tvResult.setText(String.valueOf(result));
}
}
}