学习目标:
- 文本输入框和3个按钮,分别为“生成随机数”“提交”和“返回”。
- 隐式跳转。点击“退出”按钮,退出APP。
- 用户输入一 一个0-100的数字,然后点击“提交” 当该数大(小)于系统提供的随机数时,给出Toast提醒“您输入的数字大(小)了!”, 当输入的数字等于随机数时,Toast提醒“恭喜你,答对了!点击“返回”按钮,返回到Acitivity界面。
- 要求Activity和Activity2里面均复写所有的生命周期函数,在函数内使用Log.i打印输出该函数名。在运行提示框中截图Log输出。
学习内容:
- UI页面设计
- 隐式跳转
- Toas输出
- Log打印
学习产出:
要设计两个UI界面 一个为进入程序时准备开始游戏页面,一个为进入游戏后比较大小页面
XML1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".myGame">
<Button
android:id="@+id/btn_1"
android:layout_width="200dp"
android:layout_height="100dp"
android:text="开始游戏"
android:textSize="30dp"
/>
<Button
android:id="@+id/btn_2"
android:layout_width="200dp"
android:layout_height="100dp"
android:text="退出"
android:textSize="30dp"
/>
</LinearLayout>
XML代码二
<?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:orientation="vertical">
<EditText
android:id="@+id/ed"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="60dp"
android:textSize="30dp"
android:layout_marginLeft="5dp"
android:hint="请输入一个0-100的整数"
android:numeric="integer"
/>
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="生成随机数"/>
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="提交"/>
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="返回"/>
</LinearLayout>
Java1
package com.example.class8;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class myGame extends AppCompatActivity {
private Button btn_1,btn_2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_game);
btn_1=findViewById(R.id.btn_1);
btn_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
intent.setAction("game");
startActivity(intent);
}
});
//btn_2退出
btn_2=findViewById(R.id.btn_2);
btn_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.i("生命周期函数","——————————————————onCreat——————————————————");
}
@Override
protected void onResume() {
super.onResume();
Log.i("生命周期函数","——————————————————onResume——————————————————");
}
@Override
protected void onStop() {
super.onStop();
Log.i("生命周期函数","——————————————————onStop——————————————————");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i("生命周期函数","——————————————————onRestart——————————————————");
}
@Override
protected void onPause() {
super.onPause();
Log.i("生命周期函数","——————————————————onPause——————————————————");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("生命周期函数","——————————————————onDestroy——————————————————");
}
}
小细节 Log.d和Log.i
java Activity
package com.example.class8;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class game extends AppCompatActivity {
private Button b1,b2,b3;
int num=5;
int n;
EditText ed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_two);
Log.d("生命周期函数", "——————————————————onCreate——————————————————");
b1=findViewById(R.id.b1);
b2=findViewById(R.id.b2);
b3=findViewById(R.id.b3);
ed=findViewById(R.id.ed);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = (int) (Math.random() * 100);
}
});
//点击提交 比大小 Toast输出
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String str=ed.getText().toString();
n=Integer.parseInt(str);
if(n>num){
Toast.makeText(getApplicationContext(),"您输入的数字大了!",Toast.LENGTH_LONG).show();
}
else if(n==num) {
Toast.makeText(getApplicationContext(),"恭喜你,答对了!",Toast.LENGTH_LONG).show();
}
else if(n<num){
Toast.makeText(getApplicationContext(),"您输入的数字小了!",Toast.LENGTH_LONG).show();
}
}
});
//返回
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.i("生命周期函数","——————————————————onCreat——————————————————");
}
@Override
protected void onResume() {
super.onResume();
Log.i("生命周期函数","——————————————————onResume——————————————————");
}
@Override
protected void onStop() {
super.onStop();
Log.i("生命周期函数","——————————————————onStop——————————————————");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i("生命周期函数","——————————————————onRestart——————————————————");
}
@Override
protected void onPause() {
super.onPause();
Log.i("生命周期函数","——————————————————onPause——————————————————");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("生命周期函数","——————————————————onDestroy——————————————————");
}
}