标签:user 重写 科技 warning 子类 定义 boolean ubi pac
Android开发是指android平台上应用的制作,Android早期由“Android之父”之称的Andy Rubin创办,Google于2005年并购了成立仅22个月的高科技企业Android,展开了短信、手机检索、定位等业务,以Java为编程语言,使接口到功能,都有层出不穷的变化。
第一次写博客,感觉很奇妙,想了好久,还没有思路,到底写什么呢?仍记得,初高中写作文时,假如对文题没有头绪,就会选择编个故事,把自己当作其中的主角,想其所想,悟其所悟,体会其中的冷暖寒凉,然后一篇1000字的文章就写完了。这个时候,想着想着,渐渐有所感悟。博客记录的是一个程序员进行项目开发的所思所悟,这个贵在积累,每天一点点,既是对自己的总结又能将自己的经历与网友共勉。所以现在想来,博客也并没有那么难了,因为这是为自己书写的。
注意:
1、选择合适的SDK版本
2、注意子类名的Activity后缀,这是种规范的命名方式
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/question_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="24dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/true_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/true_button"/> <Button android:id="@+id/false_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/false_button"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/prev_button" android:text="@string/prev_button"/> <Button android:id="@+id/next_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next_button"/> </LinearLayout> <Button android:id="@+id/cheat_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:text="@string/cheat_button"/> </LinearLayout>
LinearLayout有两个子组件:TextView、LinearLayout。
若作为子组件的LinearLayout,还自带两个button组件。
组件的俩属性:match_parent、wrap_content
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); mTrueButton = (Button) findViewById(R.id.true_button); mTrueButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ Toast.makeText(QuizActivity.this,R.string.incorrect_toast,Toast.LENGTH_SHORT).show(); checkAnswer(true); } }); mFalseButton = (Button) findViewById(R.id.false_button); mFalseButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ Toast.makeText(QuizActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT).show(); checkAnswer(false); } });
package edu.niit.software.geoquiz; import android.content.Context; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class CheatActivity extends AppCompatActivity { private static final String EXTRA_ANSWER_IS_TRUE="edu.niit.software.geoquiz.answer_is_true"; private static final String EXTRA_ANSWER_SHOWN = "edu.niit.software.geoquiz.answer_shown"; private boolean mAnswerIsTrue; private TextView mAnswerTextView; private Button mShowAnswerButton; 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_SHOWN,false); } @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(){ 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_SHOWN,isAnswerShown); setResult(RESULT_OK,data); } }
package edu.niit.software.geoquiz; /** * Created by 666 on 2017/9/4. */ public class Question { private int mTextResId; private boolean mAnswerTrue; public Question(int textResId,boolean answertrue){ mTextResId = textResId; mAnswerTrue = answertrue; } public int getTextResId() { return mTextResId; } public void setTextResId(int textResId) { mTextResId = textResId; } public boolean isAnswerTrue() { return mAnswerTrue; } public void setAnswerTrue(boolean answerTrue) { mAnswerTrue = answerTrue; } }
package edu.niit.software.geoquiz; import android.app.Activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class QuizActivity extends AppCompatActivity { private Button mTrueButton; private Button mFalseButton; private Button mNextButton; private Button mPrevButton; 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); mTrueButton = (Button) findViewById(R.id.true_button); mTrueButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ //Toast.makeText(QuizActivity.this,R.string.incorrect_toast,Toast.LENGTH_SHORT).show(); checkAnswer(true); } }); mFalseButton = (Button) findViewById(R.id.false_button); mFalseButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ // Toast.makeText(QuizActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT).show(); checkAnswer(false); } }); mNextButton = (Button) findViewById(R.id.next_button); mNextButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ mCurrentIndex = (mCurrentIndex+1) % mQuestionBank.length; mIsCheater = false; updateQuestion(); } }); mPrevButton = (Button) findViewById(R.id.prev_button); mPrevButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ mCurrentIndex = (mCurrentIndex-1) % mQuestionBank.length; updateQuestion(); } }); mCheatButton = (Button) findViewById(R.id.cheat_button); mCheatButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ //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 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(){ 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.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(); } 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); } } }
strings
<resources> <string name="app_name">GeoQuiz</string> <string name="question_australia">堪培拉是澳大利亚的首都 </string> <string name="question_oceans">太平洋比大西洋大</string> <string name="question_mideast">苏伊士运河连接着红海和印度洋</string> <string name="question_africa">尼罗河流域在埃及</string> <string name="question_americas">亚马逊河是美国最长的河</string> <string name="question_asia">贝加尔湖是世界上最古老最深的淡水湖</string> <string name="true_button">TRUE</string> <string name="false_button">FALSE</string> <string name="next_button">Next</string> <string name="prev_button">Prev</string> <string name="correct_toast">Correct!</string> <string name="incorrect_toast">Incorrect!</string> <string name="warning_text">Are you sure you want to do this?</string> <string name="show_answer_button">Show Answer</string> <string name="cheat_button">Cheat!</string> <string name="judgment_toast">Cheating is wrong</string> </resources>
标签:user 重写 科技 warning 子类 定义 boolean ubi pac
原文地址:http://www.cnblogs.com/xinjin/p/7538413.html