1 . 熟悉Android studio
基本界面
package com.example.a;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
小项目---飞花令
点击按钮:切按钮的标题
开始和暂停之间切换
准备候词 数组保存
开始
暂停
activity-main
<?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=".MainActivity">
<!-- 文本框ctrl+/ -->
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#58c5c7"
android:gravity="center"
android:text="飞花令"
android:textColor="#FFF"
android:textSize="30sp"
/>
<!-- 按钮-->
<Button
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="100dp"
android:layout_marginRight="50dp"
android:background="#D65489"
android:onClick="start"
android:text="开始"
android:textColor="#FFF"
android:textSize="20sp"
/>
</LinearLayout>
MainActivity
package com.example.a;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.sql.Time;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
String[] names = new String[]{"花","酒","秋风","草","桃花","燕子","雁杜","鹃雨"};
Timer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//按钮的点击事件
public void start(View view) {
// System.out.println("点击了");
//将view转换成button
Button btn = (Button) view;
//获取当前的标题
String title = btn.getText().toString();
//判断按钮的标题
if (title.equals("开始")) {
//设置为暂停
btn.setText("暂停");
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
}
},0,100);
} else {
//设置为暂停
btn.setText("开始");
timer.cancel();//关闭定时器
}
peom();
}
public void peom(){
//产生一个随机数
Random random = new Random();
int index = Math.abs(random.nextInt()) % names.length;
//从数组里面取出这个词
String name = names[index];
//将名字框显示到文本
TextView tv = findViewById(R.id.tv_name);
tv.setText(name);
}
}