---- 我确实在 setcontentview 下移动了微调器和 textview 变量,但是微调器是不公开的,所以我可以在 void onitemselected 中使用它们我应该怎么做???
好的,所以我想使用微调器从它们那里获取我的值,并将其与随机生成的值进行比较,如果它们正确,则在 textview 中将它们显示为绿色,如果不是红色,我只为一个微调器做了这个 IF 语句只是为了测试它但它强制关闭。
主页.java
package com.example.decrypter;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class MainPage extends Activity implements OnItemSelectedListener {
double random1;
int check1,check2,check3,check4,check5;
EditText textbox;
int guess;
String s;
Spinner spinner1 = (Spinner) findViewById(R.id.spinner01);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner02);
Spinner spinner3 = (Spinner) findViewById(R.id.spinner03);
Spinner spinner4 = (Spinner) findViewById(R.id.spinner04);
Spinner spinner5 = (Spinner) findViewById(R.id.spinner05);
TextView display1 = (TextView) findViewById(R.id.txtdisplay1);
TextView display2 = (TextView) findViewById(R.id.txtdisplay2);
TextView display3 = (TextView) findViewById(R.id.txtdisplay3);
TextView display4 = (TextView) findViewById(R.id.txtdisplay4);
TextView display5 = (TextView) findViewById(R.id.txtdisplay5);
Integer[] numbers = {1,2,3,4,5,6,7,8,9};
Button btnrandom = (Button) findViewById(R.id.btnrandom);
Button btn1 = (Button) findViewById(R.id.btn1);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter <Integer> adapter1 = new ArrayAdapter <Integer>(this, android.R.layout.simple_spinner_item, numbers);
setContentView(R.layout.activity_main_page);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(this);
spinner2.setAdapter(adapter1);
spinner2.setOnItemSelectedListener(this);
spinner3.setAdapter(adapter1);
spinner3.setOnItemSelectedListener(this);
spinner4.setAdapter(adapter1);
spinner4.setOnItemSelectedListener(this);
spinner5.setAdapter(adapter1);
spinner5.setOnItemSelectedListener(this);
btnrandom.setOnClickListener( new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
random1 = Math.floor(Math.random()*10);
//display.setText("random:" + random1);
/*check1 = Integer.parseInt(spinner1.getSelectedItem().toString()) ;
*/
}
});
btn1.setOnClickListener( new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
if(check1==random1){
display1.setTextColor(Color.GREEN);
display1.setText(s);
}
else{
display1.setTextColor(Color.RED);
display1.setText(s);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_page, menu);
return true;
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
check1 = Integer.parseInt(spinner1.getSelectedItem().toString());
check2 = Integer.parseInt(spinner2.getSelectedItem().toString()) ;
check3 = Integer.parseInt(spinner3.getSelectedItem().toString()) ;
check4 = Integer.parseInt(spinner4.getSelectedItem().toString()) ;
check5 = Integer.parseInt(spinner5.getSelectedItem().toString()) ;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
AndroidManifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.decrypter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainPage"
android:label="@string/title_activity_main_page" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>