0

---- 我确实在 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>

4

2 回答 2

0

仅供参考,您无需将 Layout 设置为您的活动类即可找到视图。所以把那些写在findViewsById()后面setContentView()

例如:

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_page);     //set layout 

     Spinner spinner1 = (Spinner) findViewById(R.id.spinner01);
     Spinner spinner2 = (Spinner) findViewById(R.id.spinner02);
     .....
     .....
     .....
}

还有一件事,如果您想在整个班级中访问这些所有视图,请为这些声明 Class 级别。

于 2012-11-08T09:44:24.727 回答
0

在此代码行之后将以下代码写入 onCreate() 方法setContentView(R.layout.activity_main_page);,它将解决您的问题。

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);

Button btnrandom = (Button) findViewById(R.id.btnrandom);
Button btn1 = (Button) findViewById(R.id.btn1);
于 2012-11-08T09:44:58.190 回答