如何配置 'Next' 按钮
How to configure 'Next' button
我打算制作一个测验应用程序。但我无法准确配置下一个问题按钮。我尝试制作如下所示的 promptTexts 方法。
public int promptTexts(List<String> optionAs, List<String> optionBs,
List<String> optionCs, List<String> optionDs,
List<String> questions, int i) {
i++;
txt_questionAdjective.setText(questions.get(i));
((RadioButton) grp_options.getChildAt(0)).setText(optionAs.get(i));
((RadioButton) grp_options.getChildAt(1)).setText(optionBs.get(i));
((RadioButton) grp_options.getChildAt(2)).setText(optionCs.get(i));
((RadioButton) grp_options.getChildAt(3)).setText(optionDs.get(i));
return i;
}
我的 onCreate 方法如下所示。
Button btn_nextQuestion;
RadioGroup grp_options;
TextView txt_questionAdjective;
TextView txt_optionA;
TextView txt_optionB;
TextView txt_optionC;
TextView txt_optionD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_adjectives);
// Hangi xml dosyasının dikkate alınacağı belirlendi.
final List<String> questions = new ArrayList();
final List<String> optionAs = new ArrayList();
final List<String> optionBs = new ArrayList();
final List<String> optionCs = new ArrayList();
final List<String> optionDs = new ArrayList();
// Reading json file from assets folder
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open(
"Questions.json")));
String temp;
while ((temp = br.readLine()) != null)
sb.append(temp);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close(); // stop reading
} catch (IOException e) {
e.printStackTrace();
}
}
String myjsonstring = sb.toString();
// Try to parse JSON
try {
// Creating JSONObject from String
JSONObject jsonObjMain = new JSONObject(myjsonstring);
// Creating JSONArray from JSONObject
JSONArray jsonArray = jsonObjMain.getJSONArray("questions");
// JSONArray has x JSONObject
for (int i = 0; i < jsonArray.length(); i++) {
// Creating JSONObject from JSONArray
JSONObject jsonObj = jsonArray.getJSONObject(i);
// Getting data from individual JSONObject
int id = jsonObj.getInt("id");
String question = jsonObj.getString("question");
String optionA = jsonObj.getString("optionA");
String optionB = jsonObj.getString("optionB");
String optionC = jsonObj.getString("optionC");
String optionD = jsonObj.getString("optionD");
String rightAnswer = jsonObj.getString("rightAnswer");
questions.add(jsonObj.getString("question"));
optionAs.add(jsonObj.getString("optionA"));
optionBs.add(jsonObj.getString("optionB"));
optionCs.add(jsonObj.getString("optionC"));
optionDs.add(jsonObj.getString("optionD"));
}
} catch (JSONException e) {
e.printStackTrace();
}
// The question is being prompted.
txt_questionAdjective = (TextView) findViewById(R.id.txt_questionAdjective);
txt_questionAdjective.setText(questions.get(0));
final int i = 0;
grp_options = (RadioGroup) findViewById(R.id.grp_options);
// Options are being prompted near the radioButtons.
((RadioButton) grp_options.getChildAt(0)).setText(optionAs.get(0));
((RadioButton) grp_options.getChildAt(1)).setText(optionBs.get(0));
((RadioButton) grp_options.getChildAt(2)).setText(optionCs.get(0));
((RadioButton) grp_options.getChildAt(3)).setText(optionDs.get(0));
btn_nextQuestion = (Button) findViewById(R.id.btn_nextQuestion);
btn_nextQuestion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
promptTexts(optionAs, optionBs, optionCs, optionDs, questions,i);
}
});
}
正在从JSON数据中解析数据,解析没有问题,但是关于nextQuestion按钮的代码很混乱。如果你知道一个简短的方法或者如果你看到我没有看到的东西,我将不胜感激你的帮助。
也许我误解了你的问题,但我有几点注意事项:
- 将
onCreate()
中的所有 ArrayList
移至 class 个变量。
- 不要将所有列表传递给方法
promptTexts()
。
- 将您的
onCreate()
代码分解为某些函数 - 至少一个用于读取,一个用于解析(填充数组)等。
- 我希望您在粘贴代码以外的地方使用
TextView txt_optionA
(和选项 B、C、D)。如果您不使用它们(我怀疑是这样)- 删除它们。
- 不要每次都查找 RadioButtons - 而是用它们创建一些 class 变量(如
RadioButton rbOptionA
等)。 findViewById()
查找通常很昂贵,因此如果您要多次使用视图 - 只需将其粘贴到变量中即可。
这些不会对性能产生任何影响(#2 和 #5 中的超次要除外),但会使您的代码更清晰、更易读。
编辑:将计数器 i
也提取为成员变量,不要将其作为 promptTexts()
方法的一部分传递。这样你的下一个按钮就会真正起作用:)
我打算制作一个测验应用程序。但我无法准确配置下一个问题按钮。我尝试制作如下所示的 promptTexts 方法。
public int promptTexts(List<String> optionAs, List<String> optionBs,
List<String> optionCs, List<String> optionDs,
List<String> questions, int i) {
i++;
txt_questionAdjective.setText(questions.get(i));
((RadioButton) grp_options.getChildAt(0)).setText(optionAs.get(i));
((RadioButton) grp_options.getChildAt(1)).setText(optionBs.get(i));
((RadioButton) grp_options.getChildAt(2)).setText(optionCs.get(i));
((RadioButton) grp_options.getChildAt(3)).setText(optionDs.get(i));
return i;
}
我的 onCreate 方法如下所示。
Button btn_nextQuestion;
RadioGroup grp_options;
TextView txt_questionAdjective;
TextView txt_optionA;
TextView txt_optionB;
TextView txt_optionC;
TextView txt_optionD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_adjectives);
// Hangi xml dosyasının dikkate alınacağı belirlendi.
final List<String> questions = new ArrayList();
final List<String> optionAs = new ArrayList();
final List<String> optionBs = new ArrayList();
final List<String> optionCs = new ArrayList();
final List<String> optionDs = new ArrayList();
// Reading json file from assets folder
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open(
"Questions.json")));
String temp;
while ((temp = br.readLine()) != null)
sb.append(temp);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close(); // stop reading
} catch (IOException e) {
e.printStackTrace();
}
}
String myjsonstring = sb.toString();
// Try to parse JSON
try {
// Creating JSONObject from String
JSONObject jsonObjMain = new JSONObject(myjsonstring);
// Creating JSONArray from JSONObject
JSONArray jsonArray = jsonObjMain.getJSONArray("questions");
// JSONArray has x JSONObject
for (int i = 0; i < jsonArray.length(); i++) {
// Creating JSONObject from JSONArray
JSONObject jsonObj = jsonArray.getJSONObject(i);
// Getting data from individual JSONObject
int id = jsonObj.getInt("id");
String question = jsonObj.getString("question");
String optionA = jsonObj.getString("optionA");
String optionB = jsonObj.getString("optionB");
String optionC = jsonObj.getString("optionC");
String optionD = jsonObj.getString("optionD");
String rightAnswer = jsonObj.getString("rightAnswer");
questions.add(jsonObj.getString("question"));
optionAs.add(jsonObj.getString("optionA"));
optionBs.add(jsonObj.getString("optionB"));
optionCs.add(jsonObj.getString("optionC"));
optionDs.add(jsonObj.getString("optionD"));
}
} catch (JSONException e) {
e.printStackTrace();
}
// The question is being prompted.
txt_questionAdjective = (TextView) findViewById(R.id.txt_questionAdjective);
txt_questionAdjective.setText(questions.get(0));
final int i = 0;
grp_options = (RadioGroup) findViewById(R.id.grp_options);
// Options are being prompted near the radioButtons.
((RadioButton) grp_options.getChildAt(0)).setText(optionAs.get(0));
((RadioButton) grp_options.getChildAt(1)).setText(optionBs.get(0));
((RadioButton) grp_options.getChildAt(2)).setText(optionCs.get(0));
((RadioButton) grp_options.getChildAt(3)).setText(optionDs.get(0));
btn_nextQuestion = (Button) findViewById(R.id.btn_nextQuestion);
btn_nextQuestion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
promptTexts(optionAs, optionBs, optionCs, optionDs, questions,i);
}
});
}
正在从JSON数据中解析数据,解析没有问题,但是关于nextQuestion按钮的代码很混乱。如果你知道一个简短的方法或者如果你看到我没有看到的东西,我将不胜感激你的帮助。
也许我误解了你的问题,但我有几点注意事项:
- 将
onCreate()
中的所有ArrayList
移至 class 个变量。 - 不要将所有列表传递给方法
promptTexts()
。 - 将您的
onCreate()
代码分解为某些函数 - 至少一个用于读取,一个用于解析(填充数组)等。 - 我希望您在粘贴代码以外的地方使用
TextView txt_optionA
(和选项 B、C、D)。如果您不使用它们(我怀疑是这样)- 删除它们。 - 不要每次都查找 RadioButtons - 而是用它们创建一些 class 变量(如
RadioButton rbOptionA
等)。findViewById()
查找通常很昂贵,因此如果您要多次使用视图 - 只需将其粘贴到变量中即可。
这些不会对性能产生任何影响(#2 和 #5 中的超次要除外),但会使您的代码更清晰、更易读。
编辑:将计数器 i
也提取为成员变量,不要将其作为 promptTexts()
方法的一部分传递。这样你的下一个按钮就会真正起作用:)