使用intent实现界面跳转
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(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 whichButton) {
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”
android:orientation=“horizontal”>
<Button
android:id=“@+id/start”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:textSize=“15pt”
android:text=“开始”
android:layout_weight=“1”/>
<Button
android:id=“@+id/pause”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:textSize=“15pt”
android:text=“暂停”
android:layout_weight=“1”/>
<Button
android:id=“@+id/go_on”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:textSize=“15pt”
android:text=“继续”
android:layout_weight=“1”/>
<Button
android:id=“@+id/btn1”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:gravity=“center”
android:textSize=“15pt”
android:text=“退出学习”
android:onClick=“onDialogClick”/>
<TextView
android:id=“@+id/word”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:gravity=“center”
android:textSize=“15pt”
android:text=“积跬步以至千里”
/>
<TextView
android:id=“@+id/word2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:gravity=“center”
android:textSize=“15pt”
android:text=“积怠情以致深渊”
/>
开始
暂停
继续
退出学习
方法解释见代码注释
time2Activity.java
package com.example.myapplication;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import java.util.Timer;
import java.util.TimerTask;
//import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class time2Activity extends AppCompatActivity implements OnClickListener{
private EditText inputet;//输入时间
private Button getTime;//得到输入的时间
private Button startTime;//点击开始自动减少时间的按钮
private Button stopTime;//点击开始停止自动减少时间的按钮
private TextView time;//当前时间
private int i = 0;
private Timer timer = null;//计时器
private TimerTask timerTask = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time2);
initviews();//初始化视图
setOnclick();//设置视图的点击监听
}
private void setOnclick() {
getTime.setOnClickListener(this);
startTime.setOnClickListener(this);
stopTime.setOnClickListener(this);
}
private void initviews() {
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
initviews();//初始化视图
setOnclick();//设置视图的点击监听
}
private void setOnclick() {
getTime.setOnClickListener(this);
startTime.setOnClickListener(this);
stopTime.setOnClickListener(this);
}
private void initviews() {
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-Y8vgjnu9-1715415494636)]
[外链图片转存中…(img-F7xZaiNQ-1715415494637)]
[外链图片转存中…(img-F95ZAbDY-1715415494638)]
[外链图片转存中…(img-Wb7YWzwb-1715415494639)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!