好的,所以我正在尝试创建一个活动来计算给定空间的面积。
我希望用户能够在“m”或“ft”中输入尺寸。答案需要在最后以米为单位给出。
对,现在解决问题。
下面的代码有效,问题是程序假设 num1 和 num2 都等于上次使用的微调器中选择的值。因此,如果您为 num1 输入 100 ft,然后为 num2 输入 10m,它会给出 1000m 的答案,因为您最后选择了 m 单位。它应该给出92.9的答案......
这几天一直很头疼...
谁能帮我!?!
提前致谢
private OnClickListener myClickListener = new OnClickListener()
{
public void onClick(View v) {
try{
a=Double.parseDouble(num1.getText().toString());
b=Double.parseDouble(num2.getText().toString());}
catch(Exception e)
{
if(num1.getText().length()==0)
{
num1.setError("please input width");
}
if(num2.getText().length()==0)
{
num2.setError("please input length");
}
}
if (opselected=="ft" && opselected1=="ft")
{c=((a * 0.0929) * (b * 0.0929));tv1.setText(Double.toString(c));}
else if (opselected=="m" && opselected1=="m")
{c=( a * b);tv1.setText(Double.toString(c));}
else if (opselected=="m" && opselected1=="ft")
{c= (a * (b * 0.0929));tv1.setText(Double.toString(c));}
else if (opselected=="ft" && opselected1=="m")
{c= ((a * 0.0929) * b);tv1.setText(Double.toString(c));}
else {tv1.setText("select units");}
//tv1 = (TextView)findViewById(R.id.TextView01);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.car);
tv1 = (TextView)findViewById(R.id.TextView01);
button01 = (Button)findViewById(R.id.Button01);
button01.setText("Display Air Volume");
button01.setOnClickListener(myClickListener);
num1 = (EditText)findViewById(R.id.EditText01);
num2 = (EditText)findViewById(R.id.EditText02);
spinOps = (Spinner)findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, ops);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinOps.setAdapter(adapter);
spinOps.setOnItemSelectedListener(this);
spinOps1 = (Spinner)findViewById(R.id.Spinner02);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, ops1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinOps1.setAdapter(adapter);
spinOps1.setOnItemSelectedListener(this);
}
public void onItemSelected (AdapterView<?> p,View v,int position,long id) {
opselected=ops[position];
opselected1=ops1[position];
tv1.setText("");
}
public void onNothingSelected(AdapterView<?> p) {
tv1.setText("");
}