android Spinner
package tw.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class Main extends Activity {
private TextView txtResult;
private ImageView imgComPlay;
private Spinner spn1;
private Button btn1;
private String sGame;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViewComponent();
}
private void setupViewComponent() {
imgComPlay = (ImageView)findViewById(R.id.imgComPlay);
txtResult = (TextView)findViewById(R.id.txtResult);
spn1 = (Spinner)findViewById(R.id.spn1);
btn1 = (Button)findViewById(R.id.btn1);
ArrayAdapter adapspn1List = ArrayAdapter.createFromResource(
this, R.array.spn1List, R.layout.spinner_layout);
spn1.setAdapter(adapspn1List);
spn1.setOnItemSelectedListener(spn1ItemSelLis);
btn1.setOnClickListener(btn1OnClick);
}
private Spinner.OnItemSelectedListener spn1ItemSelLis =
new Spinner.OnItemSelectedListener () {
public void onItemSelected(AdapterView parent,
View v,
int position,
long id) {
sGame = parent.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView parent){
}
};
private Button.OnClickListener btn1OnClick = new Button.OnClickListener() {
public void onClick(View v) {
int iComPlay = (int)(Math.random()*3 + 1);
// 1 – 剪刀, 2 – 石頭, 3 – 布.
if (iComPlay == 1 && spn1.equals(getString(R.string.playScissors))){
imgComPlay.setImageResource(R.drawable.scissors);
txtResult.setText(getString(R.string.result) +
getString(R.string.playerDraw));
}
else if (iComPlay == 1 && spn1.equals(getString(R.string.playStone))) {
imgComPlay.setImageResource(R.drawable.scissors);
txtResult.setText(getString(R.string.result) +
getString(R.string.playerWin));
}
else if (iComPlay == 1 && spn1.equals(getString(R.string.playNet))) {
imgComPlay.setImageResource(R.drawable.scissors);
txtResult.setText(getString(R.string.result) +
getString(R.string.playerLose));
}
else if (iComPlay == 2 && spn1.equals(getString(R.string.playScissors))) {
imgComPlay.setImageResource(R.drawable.stone);
txtResult.setText(getString(R.string.result) +
getString(R.string.playerLose));
}
else if (iComPlay == 2 && spn1.equals(getString(R.string.playStone))) {
imgComPlay.setImageResource(R.drawable.stone);
txtResult.setText(getString(R.string.result) +
getString(R.string.playerDraw));
}
else if (iComPlay == 2 && spn1.equals(getString(R.string.playNet))) {
imgComPlay.setImageResource(R.drawable.stone);
txtResult.setText(getString(R.string.result) +
getString(R.string.playerWin));
}
else if (iComPlay == 3 && spn1.equals(getString(R.string.playScissors))) {
imgComPlay.setImageResource(R.drawable.net);
txtResult.setText(getString(R.string.result) +
getString(R.string.playerWin));
}
else if (iComPlay == 3 && spn1.equals(getString(R.string.playStone))) {
imgComPlay.setImageResource(R.drawable.net);
txtResult.setText(getString(R.string.result) +
getString(R.string.playerLose));
}
else if(iComPlay == 3 && spn1.equals(getString(R.string.playNet))){
imgComPlay.setImageResource(R.drawable.net);
txtResult.setText(getString(R.string.result) +
getString(R.string.playerDraw));
}
}
};
}

1樓
1樓搶頭香
`想入非非視訊
http://a83.xx302.com