AndroidStudio——猜数字游戏(Activityintent相关)
这次实验主要是涉及到以下知识点:
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实现,有兴趣的可以百度其⽤法。最终实现的效果
下⾯是代码:
⾸先,l,主要⽤于⽤户输⼊所猜测的数字
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
安卓intent用法
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/txt"></TextView>
<EditText
android:id="@+id/input"
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    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>
l代码,主要⽤于显⽰⽤户猜测结果,所⽤时间及次数
<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"
android:text="@string/txt1"></TextView>
<EditText
android:id="@+id/et_result"
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|te </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"
android:text="@string/txt2"></TextView>
<EditText
android:id="@+id/et_time"
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|te
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"
android:text="@string/txt3"></TextView>
<EditText
android:id="@+id/et_times"
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|te        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"
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) {
setContentView(R.layout.activity_main);
this.submit = (Button)findViewById(R.id.submit);
this.input = (EditText)findViewById(R.id.input);
Calendar calendar = Instance();
//⼩时
hour = (Calendar.HOUR_OF_DAY);
//分钟
minute = (Calendar.MINUTE);
/
/秒
second = (Calendar.SECOND);
final String target = String((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",Text().toString());
intent.putExtra("hour",String(hour));
intent.putExtra("minute",String(minute));
intent.putExtra("second",String(second));
intent.putExtra("count",String(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) {
setContentView(R.layout.activity_second);
<_result = (EditText)findViewById(_result);
<_time = (EditText)findViewById(_time);
<_times= (EditText)findViewById(_times);
this.back = (Button)findViewById(R.id.back);
Intent intent = getIntent();
int target =Integer.StringExtra("target"));
int num = Integer.StringExtra("num"));
int hour = Integer.StringExtra("hour"));
int minute = Integer.StringExtra("minute"));
int second = Integer.StringExtra("second"));
int count = Integer.StringExtra("count"));
//当前时间
Calendar calendar = Instance();
//⼩时
int hour1 = (Calendar.HOUR_OF_DAY);
/
/分钟
int minute1 = (Calendar.MINUTE);
//秒
int second1 = (Calendar.SECOND);
int secondfinal = 0;//记录显⽰的秒数
secondfinal = (hour1-hour)*3600+(minute1-minute)*60+second1-second;
et_time.String(secondfinal)+"ms");
et_times.String(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();
}
});
}
主要不要忘记注册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的主题为弹窗模式,没有成功,参考⽹上的⽅式也⽊有成功(
另外其实不是很懂当secondActivity结束之后回到MainActivity,代码会从哪边开始,试图⽤debug看没有成功QAQ太菜了好啦,结束啦,希望还会有下⼀次的Android学习记录鸭!