我需要工具栏显示在所有屏幕中,但是ResultActivity. Java
启动后工具栏会消失。有谁知道如何使工具栏出现?我尝试使用getSupportActionBar().setDisplayHomeAsUpEnabled(true);
并将activity
更改为AppCompatActivity
,但我似乎无法使用后退按钮。
QuizF.Java
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.List;
public class QuizF extends Fragment {
List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
Button butNext;
View myView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.activity_quiz,container,false);
DbHelper db=new DbHelper(getActivity());
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtQuestion=(TextView)myView.findViewById(R.id.textView1);
rda=(RadioButton)myView.findViewById(R.id.radio0);
rdb=(RadioButton)myView.findViewById(R.id.radio1);
rdc=(RadioButton)myView.findViewById(R.id.radio2);
butNext=(Button)myView.findViewById(R.id.button1);
setQuestionView();
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup grp = (RadioGroup) myView.findViewById(R.id.radioGroup1);
RadioButton answer = (RadioButton) myView.findViewById(grp.getCheckedRadioButtonId());
Log.d("yourans", currentQ.getANSWER() + " " + answer.getText());
if (currentQ.getANSWER().equals(answer.getText())) {
score++;
Log.d("score", "Your score" + score);
}
if (qid < 5) {
currentQ = quesList.get(qid);
setQuestionView();
} else {
Intent intent = new Intent(getActivity(), ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
getActivity().finish();
}
}
});
return myView;
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getActivity().getMenuInflater().inflate(R.menu.activity_quiz, menu);
return true;
}
private void setQuestionView()
{
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
qid++;
}
}
ResultActivity.Java
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.widget.RatingBar;
import android.widget.TextView;
public class ResultActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_result);
//get rating bar object
RatingBar bar=(RatingBar)findViewById(R.id.ratingBar1);
bar.setNumStars(5);
bar.setStepSize(0.5f);
//get text view
TextView t=(TextView)findViewById(R.id.textResult);
//get score
Bundle b = getIntent().getExtras();
int score= b.getInt("score");
//display score
bar.setRating(score);
switch (score)
{
case 0:t.setText("Are you serious?");
break;
case 1: t.setText("Try a little harder, next time.");
break;
case 2: t.setText("Oopsie! Better Luck Next Time!");
break;
case 3:
case 4:t.setText("Hmmmm.. Someone's been reading a lot of trivia");
break;
case 5:t.setText("Who are you? A trivia wizard???");
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_result, menu);
return true;
}
}
DBhelper.Java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "triviaQuiz";
// tasks table name
private static final String TABLE_QUEST = "quest";
// tasks Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; //correct option
private static final String KEY_OPTA= "opta"; //option a
private static final String KEY_OPTB= "optb"; //option b
private static final String KEY_OPTC= "optc"; //option c
private SQLiteDatabase dbase;
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
dbase=db;
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
+ " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, "
+KEY_OPTB +" TEXT, "+KEY_OPTC+" TEXT)";
db.execSQL(sql);
addQuestions();
//db.close();
}
private void addQuestions()
{
Question q1=new Question("Which company is the largest manufacturer" +
" of network equipment?","HP", "IBM", "CISCO", "CISCO");
this.addQuestion(q1);
Question q2=new Question("Which of the following is NOT " +
"an operating system?", "SuSe", "BIOS", "DOS", "BIOS");
this.addQuestion(q2);
Question q3=new Question("Which of the following is the fastest" +
" writable memory?","RAM", "FLASH","Register","Register");
this.addQuestion(q3);
Question q4=new Question("Which of the following device" +
" regulates internet traffic?", "Router", "Bridge", "Hub","Router");
this.addQuestion(q4);
Question q5=new Question("Which of the following is NOT an" +
" interpreted language?","Ruby","Python","BASIC","BASIC");
this.addQuestion(q5);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
// Create tables again
onCreate(db);
}
// Adding new question
public void addQuestion(Question quest) {
//SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_QUES, quest.getQUESTION());
values.put(KEY_ANSWER, quest.getANSWER());
values.put(KEY_OPTA, quest.getOPTA());
values.put(KEY_OPTB, quest.getOPTB());
values.put(KEY_OPTC, quest.getOPTC());
// Inserting Row
dbase.insert(TABLE_QUEST, null, values);
}
public List<Question> getAllQuestions() {
List<Question> quesList = new ArrayList<Question>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
dbase=this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Question quest = new Question();
quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(1));
quest.setANSWER(cursor.getString(2));
quest.setOPTA(cursor.getString(3));
quest.setOPTB(cursor.getString(4));
quest.setOPTC(cursor.getString(5));
quesList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
return quesList;
}
public int rowcount()
{
int row=0;
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
row=cursor.getCount();
return row;
}
}
Question.Java
public class Question {
private int ID;
private String QUESTION;
private String OPTA;
private String OPTB;
private String OPTC;
private String ANSWER;
public Question()
{
ID=0;
QUESTION="";
OPTA="";
OPTB="";
OPTC="";
ANSWER="";
}
public Question(String qUESTION, String oPTA, String oPTB, String oPTC,
String aNSWER) {
QUESTION = qUESTION;
OPTA = oPTA;
OPTB = oPTB;
OPTC = oPTC;
ANSWER = aNSWER;
}
public int getID()
{
return ID;
}
public String getQUESTION() {
return QUESTION;
}
public String getOPTA() {
return OPTA;
}
public String getOPTB() {
return OPTB;
}
public String getOPTC() {
return OPTC;
}
public String getANSWER() {
return ANSWER;
}
public void setID(int id)
{
ID=id;
}
public void setQUESTION(String qUESTION) {
QUESTION = qUESTION;
}
public void setOPTA(String oPTA) {
OPTA = oPTA;
}
public void setOPTB(String oPTB) {
OPTB = oPTB;
}
public void setOPTC(String oPTC) {
OPTC = oPTC;
}
public void setANSWER(String aNSWER) {
ANSWER = aNSWER;
}
}
答案 0 :(得分:0)
您需要在活动布局中包含工具栏:使用“include”元素的ResultActivity。您也可以直接在布局中添加工具栏