-1

我想开发一个 aptitude 应用程序..为此在我的文本视图中我必须显示第一个问题..单击下一个按钮我必须显示第二个问题..再次单击相同的下一个按钮必须显示第三个问题..喜欢我想显示大约 30 个问题..所有问题都应该显示在单个 java 文件中。我试图显示两个问题。但是对于多个问题,我找不到代码..

  package com.example.asl;
  import java.util.Arrays;
  import java.util.Random;

 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.TextView;

  public class Aptitude extends Activity {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.aptitude);
    Button b=(Button) findViewById(R.id.button1);
    final TextView tv=(TextView) findViewById(R.id.textView1);
    final String Question[]={"what is UR Name","What is ur Age","Whats ur Qualification"};
    Button btnNext = (Button) findViewById(R.id.button1);
     final TextView cumulos = (TextView) findViewById(R.id.textView1);
        //TextView respostas = (TextView)findViewById(R.id.respostas);

        Random randPhrase = new Random();
        final String[] cum = {"what is UR Name","What is ur Age","Whats ur Qualification"};
        //String[] resp = getResources().getStringArray(R.array.resp_cumulos);

        String textout = "";
        String textresp = "";

    //Button btnPrevious = (Button) findViewById(R.id.yourPreviousbutton);

    btnNext.setOnClickListener(new OnClickListener(){

         public void onClick(View arg0) {
             int i = 0;
              if(i<cum.length-1){
                   i+=1;
                   cumulos.setText(cum[i]);
                  // respostas.setText(resp[i]);
              }

         }


    });
    //btnPrevious.setOnClickListener(new OnClickListener(){

         //public void onClick(View arg0) {
              //if(i>0){
                  // i-=1;
                 //  cumulos.setText(cum[i]);
                  // respostas.setText(resp[i]);
             // }

        // }


    //});

}

  }

enter code here
4

1 回答 1

2

初始化你的计数器onClick()总是会重置它

在外部初始化它onClick()并按原样增加它onClick()

public class Aptitude extends Activity {

   int i = 0;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aptitude);
        Button b=(Button) findViewById(R.id.button1);
        ...
   }

     public void onClick(View arg0) {  // rename arg0 to something meaningful 
                                       // like v or view for readibility
     //    int i = 0;   remove this guy
          if(i<cum.length-1){
               i+=1;
               cumulos.setText(cum[i]);

如果这不能解决您的问题,请解释问题所在,但我确定这部分给您带来了麻烦。

于 2013-10-24T17:47:49.647 回答