Button Time1_btn = (Button)findViewById(R.id.time1_btn);
Time1_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, time1Activity.class);
startActivity(intent);
}
});
MainActivity.java
package com.example.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Time1_btn = (Button)findViewById(R.id.time1_btn);
Time1_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, time1Activity.class);
startActivity(intent);
}
});
Button Time2_btn = (Button)findViewById(R.id.time2_btn);
Time2_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, time2Activity.class);
startActivity(intent);
}
});
}
}
[](()主界面布局文件
定义了两个Buttonn和两个TextView,采用线性布局
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:orientation=“vertical”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:gravity=“center_horizontal”
android:padding=“15dp”>
<Button
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:id="@+id/time1_btn"
android:textSize=“15pt”
android:text="@string/time1"/>
<Button
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:id="@+id/time2_btn"
android:textSize=“15pt”
android:text="@string/time2"/>
<TextView
android:id="@+id/word"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:gravity=“center”
android:textSize=“15pt”
android:text=“Less interests”
/>
<TextView
android:id="@+id/word2"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:gravity=“center”
android:textSize=“15pt”
android:text=“More interest”
/>
[](()主界面效果
[](()普通计时界面
采用安卓组件Chronometer
定义Chronometer 和三个Button,开始,暂停,重新开始
Chronometer ch ;
Button start ;
Button pause ;
Button restart ;
获取
//获取计时器组件
ch = (Chronometer) findViewById(R.id.test);
//获取开始按钮
start = (Button) findViewById(R.id.start) ;
//暂停计时按钮
pause = (Button) findViewById(R.id.pause);
//继续计时按钮
restart = (Button) findViewById(R.id.go_on);
ch.setBase设置开始计时时间,ch.start启动计时器
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//设置开始计时时间
ch.setBase(SystemClock.elapsedRealtime());
//启动计时器
ch.start();
pause.setEnabled(true);
restart.setEnabled(false);
start.setEnabled(false);
}
});
ch.stop暂停按钮监听器
//暂停按钮监听器
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
start.setText(“重新开始”);
ch.stop();
start.setEnabled(true);
restart.setEnabled(true);
pause.setEnabled(false);
}
});
//暂停按钮监听器
restart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
start.setText(“重新开始”);
ch.start();
start.setEnabled(true);
pause.setEnabled(true);
restart.setEnabled( 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》开源 false);
}
});
AlertDialog退出提示框设置
//退出提示框设置
public void onDialogClick(View v){
new AlertDialog.Builder(time1Activity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(“注意”)
.setMessage(“确定要退出本次学习吗?”)
.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();//Exit Activity
}
})
.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).create().show();
}
time1Activity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.content.DialogInterface;
import androidx.appcompat.app.AlertDialog;
public class time1Activity extends AppCompatActivity {
Chronometer ch ;
Button start ;
Button pause ;
Button restart ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time1);
//获取计时器组件
ch = (Chronometer) findViewById(R.id.test);
//获取开始按钮
start = (Button) findViewById(R.id.start) ;
//暂停计时按钮
pause = (Button) findViewById(R.id.pause);
//继续计时按钮
restart = (Button) findViewById(R.id.go_on);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//设置开始计时时间
ch.setBase(SystemClock.elapsedRealtime());
//启动计时器
ch.start();
pause.setEnabled(true);
restart.setEnabled(false);
start.setEnabled(false);
}
});
//暂停按钮监听器
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
start.setText(“重新开始”);
ch.stop();
start.setEnabled(true);
restart.setEnabled(true);
pause.setEnabled(false);
}
});
//暂停按钮监听器
restart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
start.setText(“重新开始”);
ch.start();
start.setEnabled(true);
pause.setEnabled(true);
restart.setEnabled(false);
}
});
//为Chronomter绑定事件监听器
ch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
//如果计时到现在超过了一小时秒
if ( SystemClock.elapsedRealtime() - ch.getBase() > 3600 * 1000) {
ch.stop();
start.setEnabled(true);
restart.setEnabled(false);
pause.setEnabled(false);
}
}
});
}
//退出提示框设置
public void onDialogClick(View v){
new AlertDialog.Builder(time1Activity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(“注意”)
.setMessage(“确定要退出本次学习吗?”)
.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int wh Android开源项目:ali1024.coding.net/public/P7/Android/git ichButton) {
finish();//Exit Activity
}
})
.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).create().show();
}
}
[](()普通计时界面布局文件
采用线性布局
<?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”
tools:context=".time1Activity"
android:orientation=“vertical”
android:gravity=“center_horizontal”>
<Chronometer
android:id="@+id/test"
android:textSize=“25pt”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content” />
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
最后
分享一份NDK基础开发资料
分享内容包括不限于高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter等全方面的Android进阶实践技术;希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!
hemas.android.com/tools"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".time1Activity"
android:orientation=“vertical”
android:gravity=“center_horizontal”>
<Chronometer
android:id="@+id/test"
android:textSize=“25pt”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content” />
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
最后
分享一份NDK基础开发资料
[外链图片转存中…(img-9pIYbF3y-1649940815700)]
分享内容包括不限于高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter等全方面的Android进阶实践技术;希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!