TQC-102 簡易下拉功能 spinner
這是TQC的102題,
程式碼為自行撰寫,
僅供參考。
其中可練習 spinner的設定,
事件處理。
package COM.TQC.GDD01;
import android.app.Activity;
import android.os.Bundle;
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;
import android.widget.Toast;
import android.widget.Toast;
public class GDD01 extends Activity {
protected TextView MyOutcome,result;
protected int volt;
private Spinner mSpinner;
private Button btn;
private EditText editText1,editText2,editText3;
double num=1;
static final String[] mDaysList=new String[] {
"110", "220"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
finview();
setspinner();
setListeners();
}
public void finview()
{
mSpinner = (Spinner)findViewById(R.id.spinner1);
btn = (Button) findViewById(R.id.button1);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
result = (TextView) findViewById(R.id.textView6);
}
public void setspinner()
{
ArrayAdapter adapter=new ArrayAdapter(this,
android.R.layout.simple_spinner_item, mDaysList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
}
public void setListeners()
{
mSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(GDD01.this, arg0.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
/*
if(arg0.getSelectedItem().toString()=="110")
{
num=110;
}
else if(arg0.getSelectedItem().toString()=="220")
{
num=220;
}
*/
}
@Override
public void onNothingSelected(AdapterView arg0) {
// TODO Auto-generated method stub
}
});
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String checkv=mSpinner.getSelectedItem().toString();
if (checkv.toString()=="110V")
{num=110;}
else if (checkv.toString()=="220V")
{num=220;}
double num1 = Double.parseDouble(editText1.getText().toString());
double num2 = Double.parseDouble(editText2.getText().toString());
double num3 = Double.parseDouble(editText3.getText().toString());
double count=num*num1/1000*num2*num3;
result.setText(String.valueOf(count));
}
});
}
}
自我LV~