gpt4 book ai didi

java - 如何统计提问次数?

转载 作者:行者123 更新时间:2023-11-30 11:35:23 24 4
gpt4 key购买 nike

我的这个 Activity 有问题和 4 个可能的答案,在每个用户回答后,它会重新加载另一个问题。我在右下角放置了一个 textView,起始值为 1/10 (rezultat.setText(counter+"/10");)。我想在每个问题之后将该值增加到 2/10、3/10 等等,但我不知道将 counter++ 放在哪里;在我的代码中,我到处都试过了。这是我的代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class Kviz extends Activity implements View.OnClickListener{

Runnable mLaunchTask = new Runnable() {
public void run() {
startActivity(getIntent());
}
};
Runnable mLaunchTaskFinish = new Runnable() {
public void run() {
finish();
}
};

Button bIzlazIzKviza, bOdgovor1, bOdgovor2, bOdgovor3, bOdgovor4;
TextView question, proba, rezultat;
int counter = 1;

private class Answer {
public Answer(String opt, boolean correct) {
option = opt;
isCorrect = correct;
}

String option;
boolean isCorrect;
}
Handler mHandler = new Handler();

final OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {

Answer ans = (Answer) v.getTag();
if (ans.isCorrect) {
mHandler.postDelayed(mLaunchTaskFinish, 1200);
mHandler.postDelayed(mLaunchTask,1000);
Intent i = new Intent("rs.androidaplikacijekvizopstekulture.TACANODGOVOR");
startActivity(i);

}else{
mHandler.postDelayed(mLaunchTaskFinish, 2200);
mHandler.postDelayed(mLaunchTask,2000);
Intent i = new Intent("rs.androidaplikacijekvizopstekulture.POGRESANODGOVOR");
startActivity(i);
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE); //full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.kviz);

Typeface dugmad = Typeface.createFromAsset(getAssets(), "Bebas.ttf");
Typeface pitanje = Typeface.createFromAsset(getAssets(), "Lobster.ttf");
bIzlazIzKviza = (Button) findViewById(R.id.bIzlazIzKviza);
rezultat = (TextView) findViewById(R.id.tvBrojPitanja);
question = (TextView) findViewById(R.id.tvPitanje);
bOdgovor1 = (Button) findViewById(R.id.bOdgovor1);
bOdgovor2 = (Button) findViewById(R.id.bOdgovor2);
bOdgovor3 = (Button) findViewById(R.id.bOdgovor3);
bOdgovor4 = (Button) findViewById(R.id.bOdgovor4);
bOdgovor1.setTypeface(dugmad);
bOdgovor2.setTypeface(dugmad);
bOdgovor3.setTypeface(dugmad);
bOdgovor4.setTypeface(dugmad);
bIzlazIzKviza.setTypeface(dugmad);
rezultat.setTypeface(dugmad);
question.setTypeface(pitanje);

TestAdapter mDbHelper = new TestAdapter(this);
mDbHelper.createDatabase();


try{ //Pokusava da otvori db
mDbHelper.open(); //baza otvorena

Cursor c = mDbHelper.getTestData();

question.setText(c.getString(1));

List<Answer> labels = new ArrayList<Answer>();

labels.add(new Answer(c.getString(2), true));
labels.add(new Answer(c.getString(3), false));
labels.add(new Answer(c.getString(4), false));
labels.add(new Answer(c.getString(5), false));

Collections.shuffle(labels);

bOdgovor1.setText(labels.get(0).option);
bOdgovor1.setTag(labels.get(0));
bOdgovor1.setOnClickListener(clickListener);

bOdgovor2.setText(labels.get(1).option);
bOdgovor2.setTag(labels.get(1));
bOdgovor2.setOnClickListener(clickListener);

bOdgovor3.setText(labels.get(2).option);
bOdgovor3.setTag(labels.get(2));
bOdgovor3.setOnClickListener(clickListener);

bOdgovor4.setText(labels.get(3).option);
bOdgovor4.setTag(labels.get(3));
bOdgovor4.setOnClickListener(clickListener);

rezultat.setText(counter+"/10");
}

finally{ // kada zavrsi sa koriscenjem baze podataka, zatvara db
mDbHelper.close();
}

bIzlazIzKviza.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
finish();
}
});
}


@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}

public void onClick(View v) {

}
}

最佳答案

“每个用户回答它都会重新加载另一个问题”是什么意思。 ?您是否使用不同的 Activity 来解决差异问题?
您可以对所有问题使用相同的 Activity ,当用户按下任何答案按钮时,只需调用一个方法

public void nextQuestion() {
counter++;
question.setText("");
bOdgovor1.setText("");
bOdgovor2.setText("");
bOdgovor3.setText("");
bOdgovor4.setText("");
//reset your next question and all four options here
rezultat.setText(counter + "/10");
}

关于java - 如何统计提问次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15170095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com