2024.4.3
package com.example.sub_find;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private TextView textViewTime;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到 TextView
textViewTime = findViewById(R.id.textViewTime);
// 初始化 Handler
handler = new Handler();
// 开始更新时间
startUpdatingTime();
Button button = (Button) findViewById(R.id.a1);
Button button1 = (Button) findViewById(R.id.a2);
Button button2 = (Button) findViewById(R.id.a3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//监听按钮,如果点击,就跳转
Intent intent = new Intent();
//前一个(MainActivity.this)是目前页面,后面一个是要跳转的下一个页面
intent.setClass(MainActivity.this, line.class);
startActivity(intent);
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, site.class);
startActivity(intent);
startUpdatingTime();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, start.class);
startActivity(intent);
startUpdatingTime();
}
});// 添加缺失的 }
}
private void startUpdatingTime() {
// 定义一个 Runnable 来更新时间
Runnable updateTimeRunnable = new Runnable() {
@Override
public void run() {
// 获取当前时间
String currentTime = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(new Date());
// 设置当前时间到 TextView
textViewTime.setText("当前时间: " + currentTime);
// 延迟一秒后再次更新时间
handler.postDelayed(this, 1000);
}
};
// 首次启动更新时间
handler.post(updateTimeRunnable);
}
@Override
protected void onDestroy() {
super.onDestroy();
// 停止更新时间,避免内存泄漏
handler.removeCallbacksAndMessages(null);
}
}
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private TextView textViewTime;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到 TextView
textViewTime = findViewById(R.id.textViewTime);
// 初始化 Handler
handler = new Handler();
// 开始更新时间
startUpdatingTime();
Button button = (Button) findViewById(R.id.a1);
Button button1 = (Button) findViewById(R.id.a2);
Button button2 = (Button) findViewById(R.id.a3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//监听按钮,如果点击,就跳转
Intent intent = new Intent();
//前一个(MainActivity.this)是目前页面,后面一个是要跳转的下一个页面
intent.setClass(MainActivity.this, line.class);
startActivity(intent);
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, site.class);
startActivity(intent);
startUpdatingTime();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, start.class);
startActivity(intent);
startUpdatingTime();
}
});// 添加缺失的 }
}
private void startUpdatingTime() {
// 定义一个 Runnable 来更新时间
Runnable updateTimeRunnable = new Runnable() {
@Override
public void run() {
// 获取当前时间
String currentTime = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(new Date());
// 设置当前时间到 TextView
textViewTime.setText("当前时间: " + currentTime);
// 延迟一秒后再次更新时间
handler.postDelayed(this, 1000);
}
};
// 首次启动更新时间
handler.post(updateTimeRunnable);
}
@Override
protected void onDestroy() {
super.onDestroy();
// 停止更新时间,避免内存泄漏
handler.removeCallbacksAndMessages(null);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
2023-04-17 虚基类应用