這次實驗主要是涉及到以下知識點:
1.Intent使用
傳值(當前Activity)
Intent intent = new Intent(this,OtherActivity.class);
Intent.putExtra(“key”,“value”);
startActivity(intent);
取值(目標Activity)
Intent intent = getIntent();
String name = intent .getStringExtra(“key”);
2.開始結束Activity
startActivity();
Finish();
那個可以從其他Activity傳值給mainActivity的方法還沒用過就不列了QAQ
3.計時器
我想的很簡單QAQ,沒有用到復雜的,就是在程序開始時獲取當前時間,每次猜測結果后獲取一下時間,兩個一減就實現了計時。網上查了下可以用核心類 Timer 和 TimerTask實現,有興趣的可以百度其用法。
最終實現的效果
下面是代碼:
首先,activity_main.xml,主要用於用戶輸入所猜測的數字
<TextView
android:id="@+id/txt"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/txt"></TextView>
<EditText
android:id="@+id/input"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:autofillHints=""
android:inputType="date|textUri|textShortMessage|textLongMessage|textAutoCorrect|numberSigned|textVisiblePassword|textWebEditText|textMultiLine|textNoSuggestions|textFilter|number|datetime|textWebEmailAddress|textPersonName|text|textPhonetic|textCapSentences|textPassword|textAutoComplete|textImeMultiLine|textPostalAddress|numberDecimal|textEmailAddress|numberPassword|textCapWords|phone|textEmailSubject|textCapCharacters|time|textWebPassword"
tools:targetApi="o"></EditText>
<Button
android:id="@+id/submit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button"></Button>
activity_second.xml代碼,主要用於顯示用戶猜測結果,所用時間及次數
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
style="?android:attr/borderlessButtonStyle"
android:text="@string/txt1"></TextView>
<EditText
android:id="@+id/et_result"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:autofillHints=""
android:inputType="date|textUri|textShortMessage|textLongMessage|textAutoCorrect|numberSigned|textVisiblePassword|textWebEditText|textMultiLine|textNoSuggestions|textFilter|number|datetime|textWebEmailAddress|textPersonName|text|textPhonetic|textCapSentences|textPassword|textAutoComplete|textImeMultiLine|textPostalAddress|numberDecimal|textEmailAddress|numberPassword|textCapWords|phone|textEmailSubject|textCapCharacters|time|textWebPassword" tools:targetApi="o"></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
style="?android:attr/borderlessButtonStyle"
android:text="@string/txt2"></TextView>
<EditText
android:id="@+id/et_time"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="date|textUri|textShortMessage|textLongMessage|textAutoCorrect|numberSigned|textVisiblePassword|textWebEditText|textMultiLine|textNoSuggestions|textFilter|number|datetime|textWebEmailAddress|textPersonName|text|textPhonetic|textCapSentences|textPassword|textAutoComplete|textImeMultiLine|textPostalAddress|numberDecimal|textEmailAddress|numberPassword|textCapWords|phone|textEmailSubject|textCapCharacters|time|textWebPassword"
android:autofillHints="" tools:targetApi="o"></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/times"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
style="?android:attr/borderlessButtonStyle"
android:text="@string/txt3"></TextView>
<EditText
android:id="@+id/et_times"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="date|textUri|textShortMessage|textLongMessage|textAutoCorrect|numberSigned|textVisiblePassword|textWebEditText|textMultiLine|textNoSuggestions|textFilter|number|datetime|textWebEmailAddress|textPersonName|text|textPhonetic|textCapSentences|textPassword|textAutoComplete|textImeMultiLine|textPostalAddress|numberDecimal|textEmailAddress|numberPassword|textCapWords|phone|textEmailSubject|textCapCharacters|time|textWebPassword"
android:autofillHints="" tools:targetApi="o"></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/back"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="?android:attr/borderlessButtonStyle"
android:text="@string/button1"></Button>
</LinearLayout>
ActivityMain.java代碼
主要實現將第一個頁面的要猜測的數字,用戶猜測的數字,用戶第一次猜測時所用時間,以及點擊提交的次數(用於統計猜測次數)通過intent傳給第二個activity
private Button submit;
private EditText input;
private int hour,minute,second,count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.submit = (Button)findViewById(R.id.submit);
this.input = (EditText)findViewById(R.id.input);
Calendar calendar = Calendar.getInstance();
//小時
hour = calendar.get(Calendar.HOUR_OF_DAY);
//分鍾
minute = calendar.get(Calendar.MINUTE);
//秒
second = calendar.get(Calendar.SECOND);
final String target = Integer.toString((int)((Math.random()*9)+1));
submit.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
count++;
Intent intent = new Intent();
intent.setClass(MainActivity.this,SecondActivity.class);
intent.putExtra("target",target);
intent.putExtra("num",input.getText().toString());
intent.putExtra("hour",Integer.toString(hour));
intent.putExtra("minute",Integer.toString(minute));
intent.putExtra("second",Integer.toString(second));
intent.putExtra("count",Integer.toString(count));
startActivity(intent);
}
});
}
SecondActivity.java
主要是通過Intent接收第一個activity所傳數據,進行處理后,得到猜測結果,猜測所用時間,猜測次數,將其顯示出來
private EditText et_result,et_time,et_times;
private Button back;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
this.et_result = (EditText)findViewById(R.id.et_result);
this.et_time = (EditText)findViewById(R.id.et_time);
this.et_times= (EditText)findViewById(R.id.et_times);
this.back = (Button)findViewById(R.id.back);
Intent intent = getIntent();
int target =Integer.parseInt(intent.getStringExtra("target"));
int num = Integer.parseInt(intent.getStringExtra("num"));
int hour = Integer.parseInt(intent.getStringExtra("hour"));
int minute = Integer.parseInt(intent.getStringExtra("minute"));
int second = Integer.parseInt(intent.getStringExtra("second"));
int count = Integer.parseInt(intent.getStringExtra("count"));
//當前時間
Calendar calendar = Calendar.getInstance();
//小時
int hour1 = calendar.get(Calendar.HOUR_OF_DAY);
//分鍾
int minute1 = calendar.get(Calendar.MINUTE);
//秒
int second1 = calendar.get(Calendar.SECOND);
int secondfinal = 0;//記錄顯示的秒數
secondfinal = (hour1-hour)*3600+(minute1-minute)*60+second1-second;
et_time.setText(Integer.toString(secondfinal)+"ms");
et_times.setText(Integer.toString(count)+"次");
if (target > num )
{
et_result.setText("猜小啦!");
}
else if(target == num)
{
et_result.setText("恭喜你猜對啦~~");
}
else
{
et_result.setText("猜大啦!");
}
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
AndroidManifest.xml
主要不要忘記注冊activity,除了mainactivity,其他都需要注冊,不然會出錯
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SecondActivity" >
<intent-filter>
<action android:name="android.intent.action.Second" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
本次實驗依舊有很多BUG(QAQ什么時候可以擁有一個無BUG的至少自己發現不了BUG的應用啊~)BUG如下:
(1)整個程序一經開始就只會有產生一個猜測的數進行一輪猜測。
(2)另外用視頻里的設置secondActivity的主題為彈窗模式,沒有成功,參考網上的方式也木有成功(https://www.jianshu.com/p/64b399dfcab0),不知道是不是彈窗模式和什么組件有沖突。
另外其實不是很懂當secondActivity結束之后回到MainActivity,代碼會從哪邊開始,試圖用debug看沒有成功QAQ太菜了
好啦,結束啦,希望還會有下一次的Android學習記錄鴨!