- 用戶界面設計
- Android 與MVC模式
- Activity生命周期
- 第二個Activity
- 心得體會
用戶界面設計
用戶界面是由組件構造而成的琼掠,它可以顯示文字或圖像混移,與用戶交互救拉,甚至布置屏幕上的其他組件氏堤。默認的activity布局是由
RelativeLayout
和TextView
兩個組件構成档插。且一個activity只能有一個LinearLayout
根元素慢蜓,作為根元素,必須指定Android XML的資源文件的命名空間(namespace)屬性郭膛。
Android 與MVC模式
Android 應用是基于MVC模式開發(fā)的晨抡,模型——視圖——控制器的架構模式。
模型對象:存儲應用的數(shù)據和業(yè)務邏輯
視圖對象:用戶界面则剃,由各類組件構成
控制器對象:包含應用的邏輯單元耘柱,響應視圖對象觸發(fā)的事件,管理視圖對象和模型對象之間的數(shù)據流動棍现。(Activity调煎、Fragment、Service)
更新視圖層
增加一個Next按鈕
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="4dp"
android:text="@string/next_button" />
更新字符串資源
<string name="next_button">Next</string>
新增問題字符串
<string name="question_australia">Canberra is the capital of Ausrtalia .</string>
<string name="question_oceans">The Pacific Ocean is larger than the Atlanic Ocean</string>
<string name="question_mideast">The Suez Canal connects the Red Sea and the Indian Ocean</string>
<string name="question_africa">The source of the Nile River is in Egypt.</string>
<string name="question_americas">The Amazon River is the longer river in the Americas.</string>
<string name="question_asia">Lake Baikal is the world\'s oldest and deepest freshwater lake.</string>
更新控制器層
增加按鈕變量及Question對象數(shù)組
private TextView mQuestionTextView;
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,true),
new Question(R.string.question_africa,true),
new Question(R.string.question_americas,true),
new Question(R.string.question_asia,true)
};
使用UpdateQuestion()
封裝公共代碼
mNextButton =(Button)findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
mCurrentIndex =(mCurrentIndex + 1)%mQuestionBank.length;
mIsCheater = false;
//int question = mQuestionBank[mCurrentIndex].getmTextResId();
//mQuestionTextView.setText(question);
updateQuestion();
}
});
mCheatButton = (Button) findViewById(R.id.cheat_button);
mCheatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Start CheatActicity
//Intent intent=new Intent(QuizActivity.this,CheatActivity.class);
boolean answerIsTrue = mQuestionBank[mCurrentIndex].ismAnswerTrue();
Intent intent = CheatActivity.newIntent(QuizActivity.this,answerIsTrue);
//startActivity(intent);
startActivityForResult(intent,REQUEST_CODE_CHEAT);
}
});
updateQuestion();
}
private void updateQuestion() {
Log.d(TAG,"updating question",new Exception());
int question = mQuestionBank[mCurrentIndex].getmTextResId();
mQuestionTextView.setText(question);
}
增加checkAnswer()
方法——判斷用戶答案的正確性轴咱,修正之前代碼的邏輯性錯誤(認為所有答案都是true)
private void checkAnswer(boolean userPressedTrue) {
boolean answerIsTrue = mQuestionBank[mCurrentIndex].ismAnswerTrue();
int messageResId = 0;
if (mIsCheater) {
messageResId = R.string.judgment_toast;
}else {
if (userPressedTrue == answerIsTrue) {
messageResId = R.string.correct_toast;
}else {
messageResId = R.string.incorrect_toast;
}
}
Toast.makeText(this,messageResId,Toast.LENGTH_SHORT).show();
}
調用checkAnswer()
方法汛蝙,在按鈕的監(jiān)聽器里
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Toast.makeText(QuizActivity.this,
// R.string.correct_toast,
//Toast.LENGTH_SHORT).show();
//Dose nothing yet,but soon!
checkAnswer(true);
}
});
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Toast.makeText(QuizActivity.this,
// R.string.incorrect_toast,
// Toast.LENGTH_SHORT).show();
//Does nothing yet, but soon!
checkAnswer(false);
}
});
Acticity的生命周期
activity的狀態(tài):不存在烈涮、停止、暫停窖剑、運行
用戶可以與當前運行狀態(tài)下的activity交互坚洽。但是任何時候只有一個activity能與用戶進行交互。
activty的生命周期回調函數(shù):onCreate()
西土、onDestory()
讶舰、onStart()
、onStop()
需了、onResume()
跳昼、onPause()
.
通過這幾種方法可以在activity的生命周期狀態(tài)發(fā)生關鍵性轉換時完成某些工作。
日志跟蹤理解activity生命周期
由于覆蓋方法會輸出日志肋乍,我們可以通過覆蓋activity生命周期方法來了解activity的狀態(tài)變化情況鹅颊。
輸出日志信息
新增一個日志常量
private static final String TAG = "QuizActivity";
為onCreate(Bundle)
方法添加日志輸出代碼
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
**Log.d(TAG,"onCreate(Bundle) calles");**
setContentView(R.layout.activity_quiz);
...
覆蓋更多生命周期方法
...
@Override
protected void onStart() {
super.onStart();
Log.d(TAG,"onStart() called");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG,"onResume() called");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG,"onPause() called");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG,"onStop() called");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestory() called");
}
...
創(chuàng)建水平模式布局
1.右鍵單擊
res
目錄后選擇New
->Android resources directory
,選擇layout
資源類型
2.將待選資源特征列表中的Orientation
單擊>>按鈕將其移動到已選資源特征區(qū)域中墓造。
3.選中Screen orentation
中的Landsacpe
選項堪伍,目錄名為layout-land
。
- Android 視圖下的看不到
res/layout-land
目錄觅闽,需要切換到Project視圖下帝雇。
第二個Activity
一個activity控制一屏信息,新activity是用來方便用戶偷看答案的
- 啟動activity:基于intent的通信——intent對象是compoment用來與操作系統(tǒng)通信的一種媒介工具
- activity的數(shù)據傳遞:使用intent extra ,從子activity獲取返回結果
具體代碼如下:
QuizActivity.java
private static final String KEY_INDEX = "index";
private static final int REQUEST_CODE_CHEAT = 0;
...
mCheatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Start CheatActicity
//Intent intent=new Intent(QuizActivity.this,CheatActivity.class);
boolean answerIsTrue = mQuestionBank[mCurrentIndex].ismAnswerTrue();
Intent intent = CheatActivity.newIntent(QuizActivity.this,answerIsTrue);
//startActivity(intent);
startActivityForResult(intent,REQUEST_CODE_CHEAT);
}
});
@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);
}
}
...
CheatActivity.java
private static final String EXTRA_ANSWER_IS_TRUE ="cn.happy.qxy.geoquiz.answer_is_true";
private boolean mAnswerIsTrue;
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_IS_TRUE,false);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cheat);
mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE,false);
mAnswerTexView = (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 view) {
if (mAnswerIsTrue) {
mAnswerTexView.setText(R.string.true_button);
}else {
mAnswerTexView.setText(R.string.false_button);
}
setAnswerShownResult(true);
}
});
}
private void setAnswerShownResult(boolean isAnswerShown) {
Intent data = new Intent();
data.putExtra(EXTRA_ANSWER_IS_TRUE,isAnswerShown);
setResult(RESULT_OK,data);
}
心得體會
以上就是本次項目一些重要的步驟蛉拙,及詳細的代碼展示尸闸。麻雀雖小,組臟俱全孕锄。
了解到Android Studio 的獨有的優(yōu)勢:
- 速度優(yōu)于Eclipse
- 提示補全更加人性化
- 支持Google Cloud Platform
- 強大的UI編輯器
- 受歡迎的第三方GenyMoton虛擬機
- Android Studio特有的異常調試工具Android Lint