-1

我无法向我的应用程序中的下一个按钮添加功能。它应该在每次按下时更改文本。但是,就我而言,它所做的只是更改一次文本然后进入睡眠状态!我应该怎么办?我搜索了网站,但找不到任何方法!

Java代码!

package com.dreamgoogle.gihf;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class Quotes extends Activity {

    ImageButton next;
    ImageButton previous;
    ImageButton copytext;
    TextView q;
    TextView nm;
    String[] str;
    int i;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quotes);
        varSet();

        next.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                int i = 0;
                i = i + 1;
                q.setText(str[i]);

            }

        });

    }

    private void varSet() {
        next = (ImageButton) findViewById(R.id.next);
        previous = (ImageButton) findViewById(R.id.previous);
        copytext = (ImageButton) findViewById(R.id.copy);
        q = (TextView) findViewById(R.id.quotes);
        str = new String[] {
                "In order to succeed, your desire for success should be greater than your fear of failure.",
                "A successful man is one who can lay a firm foundation with the bricks others have thrown at him.",
                "Success is not final, failure is not fatal: it is the courage to continue that counts.",
                "Try not to become a man of success. Rather become a man of value.",
                "Want Something? Go Get It!" };

    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.quotes, menu);
        return true;

    }

}

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/home"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Quotes" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/h1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/quotes"
        android:layout_width="300dp"
        android:layout_height="250dp"
        android:layout_centerInParent="true"
        android:background="@android:color/transparent"
        android:gravity="center_vertical|center_horizontal|top"
        android:text="@string/h2"
        android:textSize="12sp"
        android:typeface="monospace" />

    <ImageButton
        android:id="@+id/previous"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/quotes"
        android:layout_marginTop="14dp"
        android:layout_toLeftOf="@+id/textView1"
        android:background="@android:color/transparent"
        android:contentDescription="@drawable/previous"
        android:src="@drawable/previous" />

    <ImageButton
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/previous"
        android:layout_toRightOf="@+id/textView1"
        android:background="@android:color/transparent"
        android:contentDescription="@drawable/next"
        android:src="@drawable/next" />

    <ImageButton
        android:id="@+id/copy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/previous"
        android:layout_centerHorizontal="true"
        android:background="@android:color/transparent"
        android:contentDescription="@drawable/copy"
        android:src="@drawable/copy" />

    <TextView
        android:id="@+id/nm"
        android:layout_width="20dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/quotes"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="18dp"
        android:clickable="false"
        android:ems="10"
        android:text="@string/h3" />

</RelativeLayout>
4

2 回答 2

3

改变:

@Override
public void onClick(View v) {
    int i = 0;
    i = i + 1;
    q.setText(str[i]);
}

@Override
public void onClick(View v) {
    i = i + 1;
    q.setText(str[i]);
}

通过i在 onClick() 方法中再次声明,您可以在 Activity 类中隐藏它。并且由于每次调用此方法,i总是重置为 0,因此您的文本将始终是str[1]

这称为变量隐藏

于 2013-04-01T06:40:54.400 回答
0

您在 onClick 中声明迭代器。单击时会发生什么:

1)您创建一个值为0的新int;

2) 0 = 0 + 1;

3) 每次都将 q 的文本设置为 str[1]。

将您的迭代器声明为 OnClickListener 或整个 Activity 本身的类变量。

于 2013-04-01T06:43:10.283 回答