4

我正在尝试制作一个简单的报价应用程序,但是每次我在下一个按钮之后点击后退按钮时,它都会转到下一个数组而不是倒退。我使用 int num 来跟踪用户所在的报价。这是我的后退按钮

package me.me.quote;


public class Startme extends Activity {
 Random gen= new Random();
int num;

 String[] quotes = new String[15];





public void next(View N){
         quotes[0]= "Love is timeless";
            quotes[1]= "I hate you";
            quotes[2]= "arggg!";
            quotes[3]= "The end is near";
            quotes[4]= "Leave be";

            if(num == 5){
                num =0;}



           TextView text = (TextView) findViewById(R.id.Quote);
           text.setText(quotes[num++]);



           TextView number = (TextView) findViewById(R.id.Qn);
            number.setText(""+num);

        }
public void back(View B){

 quotes[0]= "Love is timeless";
   quotes[1]= "I hate you";
   quotes[2]= "arggg!";
   quotes[3]= "The end is near";
   quotes[4]= "Leave be";

 TextView text = (TextView) findViewById(R.id.Quote);
 text.setText(quotes[num--]);

 TextView number = (TextView) findViewById(R.id.Qn);
    number.setText(""+num);}




  public void rand(View G)  {

 num = gen.nextInt(5);

  quotes[0]= "Love is timeless";
  quotes[1]= "I hate you";
  quotes[2]= "arggg!";
  quotes[3]= "The end is near";
  quotes[4]= "Leave be";
  TextView text = (TextView) findViewById(R.id.Quote);
  text.setText(quotes[num]);

TextView number = (TextView) findViewById(R.id.Qn);
number.setText(""+num);         
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.startquotes);

    TextView Dark=(TextView)findViewById(R.id.Quote); 
    Typeface face=Typeface.createFromAsset(getAssets(), "fonts/font1.ttf"); 
    Dark.setTypeface(face); 


}


}
4

1 回答 1

4

//布局文件 text.xml

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_alignParentTop="true"
            android:text="VISIBLE EVERY TIME"
            android:visibility="visible" />

        <Button
            android:id="@+id/back"
            android:layout_width="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"
            android:text="NEXT" >
        </Button>

        <Button
            android:id="@+id/next"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"
            android:text="BACK" >
        </Button>

    </RelativeLayout>

///活动类

 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 Test extends Activity {

        Button next;
        Button back ;
        TextView quoteText;
        int currentSelectedQuote=0;
        String[] quote={"QUOTE1","QUOTE2","QUOTE3","QUOTE4","QUOTE5",};

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);

            setContentView(R.layout.text);

            quoteText=(TextView)findViewById(R.id.textView);


            quoteText.setText(quote[currentSelectedQuote]);

             next=(Button)findViewById(R.id.next);

             next.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    currentSelectedQuote++;


                    if(currentSelectedQuote == 5){
                        currentSelectedQuote =0;}

                    quoteText.setText(quote[currentSelectedQuote]);

                }
            });
             back=(Button)findViewById(R.id.back); 

             back.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    if(currentSelectedQuote==0)
                    {
                        currentSelectedQuote=4;
                    }
                    else
                    {
                    currentSelectedQuote--;
                    }

                    quoteText.setText(quote[currentSelectedQuote]);

                    // TODO Auto-generated method stub

                }
            });





        }

    }
于 2012-07-16T22:34:38.923 回答