项目xml文件的下一个按钮-android



我有一个包含500个文本的xml文件。我有一个按钮,它随机抽取一个哨兵并显示出来。我想为next和back再添加两个按钮。带走每一个哨兵。这是我的rando按钮代码。

final TextView tv = (TextView) findViewById(R.id.quote);
        Resources res = getResources();
        myString = res.getStringArray(R.array.quote);
        String q = myString[rgenerator.nextInt(myString.length)];
        tv.setText(q);
        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                myString = getResources().getStringArray(R.array.quote);
                String q = myString[rgenerator.nextInt(myString.length)];
                tv.setText(q);
            }
        });
    }

这些按钮的代码是什么?谢谢

所有代码:

import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
public class MainActivity extends ActionBarActivity {
    Context context;
    private String[] myString;
    private static final Random rgenerator = new Random();
    private AdView adView;
    int index = 0;
    private static final String AD_UNIT_ID = "ca-app-pub-7628432187347131/3277094808";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
        // Create an ad.
        adView = new AdView(this);
        adView.setAdSize(AdSize.SMART_BANNER);
        adView.setAdUnitId(AD_UNIT_ID);
        context = this;
        LinearLayout layout = (LinearLayout) findViewById(R.id.ads_lin);
        layout.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("5B895A3CC0CA50D56506E300A4C8342B")
                .addTestDevice("D039292A1F434C999B21503D63D6FD88")
                .addTestDevice("TEST_EMULATOR").build();
        // Start loading the ad in the background.
        adView.loadAd(adRequest);
        // Start loading the ad in the background.
        final TextView tv = (TextView) findViewById(R.id.quote);
        Resources res = getResources();
        myString = res.getStringArray(R.array.quote);
        String q = myString[rgenerator.nextInt(myString.length)];
        tv.setText(q);
        Button btn = (Button) findViewById(R.id.btn);
        Button btnNext = (Button) findViewById(R.id.btnNext);
        Button btnPreview = (Button) findViewById(R.id.btnPreview);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                myString = getResources().getStringArray(R.array.quote);
                index = rgenerator.nextInt(myString.length);
                String q = myString[index];
                tv.setText(q);
            }
        });
        btnNext.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String q = myString[index++];
                tv.setText(q);
            }
        });
        btnPreview.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                String q = myString[index--];
                        tv.setText(q);
            }
        });
    }
    @Override
    public void onResume() {
        super.onResume();
        if (adView != null) {
            adView.resume();
        }
    }
    @Override
    public void onPause() {
        if (adView != null) {
            adView.pause();
        }
        super.onPause();
    }
    /** Called before the activity is destroyed. */
    @Override
    public void onDestroy() {
        // Destroy the AdView.
        if (adView != null) {
            adView.destroy();
        }
        super.onDestroy();
    }
}

没有错误,但当我运行应用程序时,它停止了。

这是logcat:

09-20 17:24:53.341: E/dalvikvm(7651): adjustAdaptiveCoef max=6291456, min=1572864, ut=568
09-20 17:24:54.152: E/AndroidRuntime(7651): FATAL EXCEPTION: main
09-20 17:24:54.152: E/AndroidRuntime(7651): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.stiaica/com.example.stiaica.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.app.ActivityThread.access$700(ActivityThread.java:150)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.os.Looper.loop(Looper.java:137)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.app.ActivityThread.main(ActivityThread.java:5283)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at java.lang.reflect.Method.invokeNative(Native Method)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at java.lang.reflect.Method.invoke(Method.java:511)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at dalvik.system.NativeStart.main(Native Method)
09-20 17:24:54.152: E/AndroidRuntime(7651): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
09-20 17:24:54.152: E/AndroidRuntime(7651):     at com.example.stiaica.MainActivity.onCreate(MainActivity.java:59)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.app.Activity.performCreate(Activity.java:5283)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
09-20 17:24:54.152: E/AndroidRuntime(7651):     ... 11 more
09-20 17:24:54.252: E/GooglePlayServicesUtil(7651): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.

我试图用"活动"更改ActionBarActivity,但出现了更多错误。

现在正在工作,按钮正在工作。但有一个问题,当我到达最后一个岗哨并按下下一个按钮时,应用程序停止了。第一次岗哨也是如此。

LOGCAT:

09-20 18:08:56.915: E/dalvikvm(20667): adjustAdaptiveCoef max=4194304, min=1048576, ut=568
09-20 18:08:58.937: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:08:59.508: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:08:59.548: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:08:59.568: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:09:31.949: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:09:32.020: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:09:32.040: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:09:32.040: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:10:03.740: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:10:03.841: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:10:03.861: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:10:03.871: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:10:34.871: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:10:34.931: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:10:34.961: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:10:34.961: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:10:40.226: E/AndroidRuntime(20667): FATAL EXCEPTION: main
09-20 18:10:40.226: E/AndroidRuntime(20667): java.lang.ArrayIndexOutOfBoundsException: length=593; index=593
09-20 18:10:40.226: E/AndroidRuntime(20667):    at com.example.stiaica.MainActivity$2.onClick(MainActivity.java:73)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at android.view.View.performClick(View.java:4432)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at android.view.View$PerformClick.run(View.java:18338)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at android.os.Handler.handleCallback(Handler.java:725)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at android.os.Looper.loop(Looper.java:137)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at android.app.ActivityThread.main(ActivityThread.java:5283)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at java.lang.reflect.Method.invokeNative(Native Method)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at java.lang.reflect.Method.invoke(Method.java:511)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at dalvik.system.NativeStart.main(Native Method)

您需要将生成的随机数存储在类似的索引变量中

移动变量的下一个增量

向后移动递减

编辑

以下是操作方法:

        public class MainActivity extends Activity {
        final int index = 0;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final TextView tv = (TextView) findViewById(R.id.quote);
        Resources res = getResources();
        final myString = res.getStringArray(R.array.quote);
        final String q = myString[rgenerator.nextInt(myString.length)];
        tv.setText(q);
        Button btnNext = (Button) findViewById(R.id.btnNext);
        Button btnPrevious = (Button) findViewById(R.id.btnPrevious);
        Button btn = (Button) findViewById(R.id.btn);
        btnNext.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String q = myString[index++];
                tv.setText(q);
            }
        });
        btnPrevious.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String q = myString[index--];
                tv.setText(q);
            }
        });
       btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            myString = getResources().getStringArray(R.array.quote);
            index = rgenerator.nextInt(myString.length);
            String q = myString[index];
            tv.setText(q);
        }
    });
    }
    }}

将所有文本放入数组列表。随机生成文本。对于next和back,获取随机生成的文本的位置,next增加位置+1,back减少位置1。

查看这篇文章中的评论,它会让你知道如何使用arraylist来处理文本。希望它能帮助你。干杯!:)按钮上的随机文本点击

最新更新