码迷,mamicode.com
首页 > 其他好文 > 详细

GeoQuiz项目的开发过程及总结

时间:2017-09-17 17:43:11      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:cheat   boolean   nsa   eoq   check   开发   blank   nal   images   

GeoQuiz项目的开发过程

1.用户界面的设计(xml文件)
(1)activity_quiz界面

技术分享

(2)activity_cheat界面

技术分享

(3)activity_crime界面

技术分享

 

 

(4)fragment_crime_list界面

技术分享

(5)list_item_crime界面

技术分享

2.用户界面的功能实现(创建类)

 (1)quizActivity

public class QuizActivity extends AppCompatActivity {
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private Button mCheatButton;
private TextView mQuestionTextView;

private static final String TAG = "QuizActivity";
private static final String KEY_INDEX = "index";
private static final int REQUEST_CODE_CHEAT = 0;

private Question[] mQuestionBank = new Question[]{
new Question(R.string.question_australia, true),
new Question(R.string.question_oceans, true),
new Question(R.string.question_mideast, false),
new Question(R.string.question_africa, false),
new Question(R.string.question_americas, true),
new Question(R.string.question_asia, true)
};
private int mCurrentIndex = 0;
private boolean mIsCheater;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate(Bundle)called");
setContentView(R.layout.activity_quiz);

if (savedInstanceState != null) {
mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
}

mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
//int question = mQuestionBank[mCurrentIndex].getTextResId();
//mQuestionTextView.setText(question);

mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(QuizActivity.this,
//R.string.correct_toast,
// Toast.LENGTH_SHORT).show();
checkAnswer(true);
}
});

mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(QuizActivity.this,
// R.string.incorrect_toast,
//Toast.LENGTH_SHORT).show();
checkAnswer(false);
}
});
mNextButton = (Button) findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
//int question = mQuestionBank[mCurrentIndex].getTextResId();
// mQuestionTextView.setText(question);
mIsCheater = false;
updateQuestion();
}
});
mCheatButton = (Button) findViewById(R.id.cheat_button);
mCheatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Start CheatActivity
//Intent intent = new Intent(QuizActivity.this, CheatActivity.class);
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswertrue();
Intent intent = CheatActivity.newIntent(QuizActivity.this, answerIsTrue);
//startActivity(intent);
startActivityForResult(intent, REQUEST_CODE_CHEAT);
}
});
updateQuestion();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == REQUEST_CODE_CHEAT)

{
if (data == null) {
return;
}
mIsCheater = CheatActivity.wasAnswerShown(data);
}

}

@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart() called");
}

@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume() called");
}

@Override
public void onPause() {
super.onPause();
Log.d(TAG, "onPause() called");
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Log.i(TAG, "onSaveInstanceState");
savedInstanceState.putInt(KEY_INDEX, mCurrentIndex);
}

@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop() called");
}

@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy() called");
}

private void updateQuestion() {
//Log.d(TAG,"Updating question text",new Exception());
int question = mQuestionBank[mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);
}

private void checkAnswer(boolean userPressedTrue) {
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswertrue();
int messageResId = 0;
if (mIsCheater) {
messageResId = R.string.judgement_toast;
} else {
if (userPressedTrue == answerIsTrue) {
messageResId = R.string.incorrect_toast;
} else {
messageResId = R.string.incorrect_toast;
}
Toast.makeText(this, messageResId, Toast.LENGTH_SHORT)
.show();
}
}

}


(2)Question

public class Question {
private int mTextResId;
private boolean mAnswertrue;

public int getTextResId() {
return mTextResId;
}

public void setTextResId(int textResId) {
mTextResId = textResId;
}

public boolean isAnswertrue() {
return mAnswertrue;
}

public void setAnswertrue(boolean answertrue) {
mAnswertrue = answertrue;
}

public Question(int textResId, boolean answerTrue) {
mTextResId = textResId;
mAnswertrue = answerTrue;
}
}

(3)CheatActivity

public class CheatActivity extends AppCompatActivity {
private static final String EXTRA_ANSWER_IS_TRUE =
"com.bignerdranch.android.geoquiz.answer_is_true";
private static final String EXTRA_ANSWER_SHOW =
" com.bignerdranch.android.geoquiz.answer_shown";


public static Intent newIntent(Context packageContext, boolean answerIsTrue) {
Intent intent = new Intent(packageContext, CheatActivity.class);
intent.putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue);
return intent;
}
public static boolean wasAnswerShown(Intent result){
return result.getBooleanExtra(EXTRA_ANSWER_SHOW,false);
}

private boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswerButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cheat);
mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
mAnswerTextView = (TextView) findViewById(R.id.answer_text_view);
mShowAnswerButton = (Button) findViewById(R.id.show_answer_button);
mShowAnswerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mAnswerIsTrue) {
mAnswerTextView.setText(R.string.true_button);
} else {
mAnswerTextView.setText(R.string.false_button);
}
setAnswerShownResult(true);
}
});
}

private void setAnswerShownResult(boolean isAnswerShown) {
Intent data = new Intent();
data.putExtra(EXTRA_ANSWER_SHOW, isAnswerShown);
setResult(RESULT_OK, data);
}
}

(4)Crime

public class Crime {
private UUID mId;
private String mTitle;
private Date mDate;
private boolean mSolved;

public Crime() {
mId = UUID.randomUUID();
mDate = new Date();
}

public UUID getId() {
return mId;
}

public void setId(UUID id) {
mId = id;
}

public String getTitle() {
return mTitle;
}

public void setTitle(String title) {
mTitle = title;
}

public Date getDate() {
return mDate;
}

public void setDate(Date date) {
mDate = date;
}

public boolean isSolved() {
return mSolved;
}

public void setSolved(boolean solved) {
mSolved = solved;
}
}


(5)crimeActivity

public class CrimeActivity extends SingleFragmentActivity {

// @Override
// protected void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_fragment);
// FragmentManager fm=getSupportFragmentManager();
// Fragment fragment =fm.findFragmentById(R.id.fragment_container);
// if(fragment==null){
// fragment=new CrimeFragment();
// fm.beginTransaction()
// .add(R.id.fragment_container,fragment)
// .commit();
// }
// }
@Override
protected Fragment createFragment() {
return new CrimeFragment();
}
}

(6)CrimeFragment

public class CrimeFragment extends Fragment {
private Crime mCrime;
private EditText mTitleField;
private Button mDataButton;
private CheckBox mSolvedCheckBox;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCrime = new Crime();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_crime, container, false);
mTitleField = (EditText) v.findViewById(R.id.crime_title);
mTitleField.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//This space intentionally left blank
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
//This one too
}
});
mDataButton = (Button) v.findViewById(R.id.crime_date);
mDataButton.setText(mCrime.getDate().toString());
mDataButton.setEnabled(false);
mSolvedCheckBox = (CheckBox) v.findViewById(R.id.crime_solved);
mSolvedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

mCrime.setSolved(isChecked);
}
});

return v;
}

}

(7)CrimeLab

public class CrimeLab {
private static CrimeLab sCrimeLab;
private List<Crime> mCrimes;

public CrimeLab(Context context) {
mCrimes = new ArrayList<>();
for (int i = 0; i < 100; i++) {
Crime crime = new Crime();
crime.setTitle("Crime#" + i);
crime.setSolved(i % 2 == 0);//Every other one
mCrimes.add(crime);
}
}

public List<Crime> getCrimes() {
return mCrimes;
}

public Crime getCrime(UUID id) {
for (Crime crime : mCrimes) {
if (crime.getId().equals(id)) {
return crime;
}
}
return null;
}

public static CrimeLab get(Context context) {
if (sCrimeLab == null) {
sCrimeLab = new CrimeLab(context);
}
return sCrimeLab;
}
}

(8)crimeListActivity

public class CrimeListActivity extends SingleFragmentActivity {
@Override
protected Fragment createFragment() {
return new CrimeListFragment();
}
}

(9)CrimeListFragment

public class CrimeListFragment extends Fragment {
//Nothing yet
private class CrimeHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
private TextView mTitleTextView;
private TextView mDateTextView;
private Crime mCrime;

public void bind(Crime crime) {
mCrime = crime;
mTitleTextView.setText(mCrime.getTitle());
mDateTextView.setText(mCrime.getDate().toString());

}

public CrimeHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.list_item_crime, parent, false));
itemView.setOnClickListener(this);
mTitleTextView = (TextView) itemView.findViewById(R.id.crime_title);
mDateTextView = (TextView) itemView.findViewById(R.id.crime_date);
}

@Override
public void onClick(View v) {
Toast.makeText(getActivity(),
mCrime.getTitle() + "clicked", Toast.LENGTH_SHORT)
.show();
}
}

private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {
private List<Crime> mCrimes;

public CrimeAdapter(List<Crime> crimes) {
mCrimes = crimes;
}


@Override
public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
return new CrimeHolder(layoutInflater, parent);
}

@Override
public void onBindViewHolder(CrimeHolder holder, int position) {
Crime crime = mCrimes.get(position);
holder.bind(crime);
}

@Override
public int getItemCount() {
return mCrimes.size();
}
}

private RecyclerView mCrimeRecyclerView;
private CrimeAdapter mAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
mCrimeRecyclerView = (RecyclerView) view
.findViewById(R.id.crime_recycler_view);
mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return view;
}

private void updateUI() {
CrimeLab crimeLab = CrimeLab.get(getActivity());
List<Crime> crimes = crimeLab.getCrimes();
mAdapter = new CrimeAdapter(crimes);
mCrimeRecyclerView.setAdapter(mAdapter);
}
}

(10)SingleFragmentActivity

public abstract class SingleFragmentActivity extends AppCompatActivity {
protected abstract Fragment createFragment();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = createFragment();
fm.beginTransaction()
.add(R.id.fragment_container, fragment)
.commit();
}
}
}

2.总结

通过这两周的学习,对于安卓的基本开发流程有了初步的了解。收获了很多,

尤其是养成了遇到问题时(运行错误)能冷静考虑去不断运行测试代码来一个

个的解决问题,但是能力有限,并不是所以的问题都能得到有效的解决,在接

下来的阶段里有待提高。

 

GeoQuiz项目的开发过程及总结

标签:cheat   boolean   nsa   eoq   check   开发   blank   nal   images   

原文地址:http://www.cnblogs.com/nishiweiyj/p/7536128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!