点击打开链接首先我们要做的是将界面写出来。
对于界面这块我向来不咋讲究,简单粗暴。一个ScrollView滚动视图统领全屏,为了防止手机太小装不下。代码如下
<?xml version="1.0"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
当然,别忘记最后加上</XXX>
接下来是一个网格视图
<TableLayout
android:layout_height="fill_parent"android:layout_width="fill_parent"
>
为什么用网格布局呢,主要是因为方便在他身上插入……其他视图
下面我们将用N个线性布局,实现我们的MainActivity的上半部分。XML代码简单明了,就是有点臭长,不过没关系,咱的追求不是界面。
解析:第一个线性布局,重点是添加一个背景图片,占据一席之地。然后在这个线性布局中嵌套了3个子线性布局,其中每一个子线性布局只包含一个ImageView和一个TextView,TextView主要是用来显示我们的锁屏时长,解锁次数,锁屏次数。因此要注意TextView的id哦,要起一个不重复的名字。至于控件的大小、控件之间距离的远近,爱咋弄咋弄啦。
好吧,我还是在代码后台说明一下吧。
<LinearLayout
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" <!--垂直的线性布局--!>
android:background="@drawable/r_bg"<!--添加背景,这里是一张图片--!>
>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<ImageView
android:id="@+id/imageView1"
android:layout_marginTop="15dp" <!--该控件距离顶部(上一个控件)15dp--!>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" <!--各自平分剩余的重量--!>
android:src="@drawable/suo"
/>
<TextView
android:id="@+id/lockNum"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"<!--文字的显示位置为textView的左边--!>
android:textSize="20dp" <!--文字大小--!>
android:layout_marginTop="30dp"
android:text=" 0次" />
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:src="@drawable/yaoshi"
/>
<TextView
android:id="@+id/unlockNum"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textSize="20dp"
android:layout_marginTop="25dp"
android:text=" 0次" />
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<ImageView
android:id="@+id/imageView3"
android:layout_marginTop="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/shalou"
/>
<TextView
android:id="@+id/howLongToLock"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textSize="20dp"
android:layout_marginTop="35dp"
android:text=" 0分钟" />
</LinearLayout>
上半身非常简单,下面我们将下半部分造出来
因为我们剩下的布局只需要一行一行消耗空间即可,因为每一行可能会有2个孙子控件,所以理所当然地选择TableLayout的子控件TableRow啦。
其实也就第一个tableRow里有一个button,和一个选择ToggleButton,其他的TableRow都是不需要的。然而我懒得删除咯。
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/tableRow2">
<Button
android:layout_height="50dp"
android:layout_width="wrap_content"
android:id="@+id/timingButton"
android:text="定时锁屏(设置时间、日期)"
android:gravity="left"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"/>
<ToggleButton
android:layout_height="50dp"
android:layout_width="wrap_content"
android:id="@+id/timingToggleButton1"
android:layout_marginTop="12dp"
android:layout_weight="1"
android:textOn="运行中"
android:textOff="未开启"
android:checked="false"/>
</TableRow>
<View
android:layout_width="wrap_content"
android:layout_height="0.5dp"
android:background="#22000000"
/>
-<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/tableRow3">
<Button
android:layout_height="50dp"
android:layout_width="wrap_content"
android:id="@+id/fastlock"
android:text="一键锁屏"
android:gravity="left"
android:layout_weight="1"/>
</TableRow>
<View
android:layout_width="wrap_content"
android:layout_height="0.5dp"
android:background="#22000000"
/>
-<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/tableRow4">
<Button
android:layout_height="50dp"
android:layout_width="wrap_content"
android:id="@+id/userButton"
android:text="使用说明"
android:gravity="left"
android:layout_weight="1"
/>
</TableRow>
XML的最后。直接往tableLayout里添加几个textView即可。这里不粘贴出来。
还是多说一句吧。这个View的作用就是一个分割线。
<View
android:layout_width="wrap_content"
android:layout_height="0.5dp"
android:background="#22000000"
/>
下面就是Activity的实现了。不废话。直接上代码,不懂?给你加个注释
package com.example.mylock;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
/*
* 进入程序的第一个界面,其功能按钮有 定时锁屏设置时间按钮 定时锁屏开关按钮 一键锁屏按钮 使用说明按钮
* 另外在这个界面会显示从SP文件中获取的定时锁屏的日期与时间(前提是启动了定时锁屏)
*/
public class MainActivity extends Activity {
private ToggleButton timingToggleButton;// 模式开关按钮,
private Button timingSetButton, fastButton, userButton;// 模式设置按钮
private TextView[] ShowTimeTextView;// 显示某个模式设置的时间段的文本框
private TextView DayOflock, lockNum, unlockNum, lockTime;
boolean status;// 存贮开关的布尔值
private static Context mycontext;
private getDataFromSp gdfs;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);// 重写父类方法
setContentView(R.layout.activity_main);// 设置activity
// getWindow().setBackgroundDrawableResource(R.drawable.a1);//为activity添加个背景图
this.init();// 调用初始化方法
this.getModelStatus();// 从SP文件中获取togglebutton按钮的状态,也就是一键锁屏是否在启动
SetLockAndUnlockAndLockTime();
this.setButtonOnListener();// 调用为按钮设置点击事件的方法
}
/*
* 初始化方法,实例化各个数据
*/
public void init() {
ShowTimeTextView = new TextView[5]; // 五个文本输出时间段
mycontext = this.getApplicationContext();
gdfs = new getDataFromSp(mycontext);
// 分别从xml文件中获取各个按钮
timingSetButton = (Button) findViewById(R.id.timingButton);
timingSetButton.setBackgroundColor(Color.WHITE);
timingToggleButton = (ToggleButton) findViewById(R.id.timingToggleButton1);
timingToggleButton.setBackgroundColor(Color.WHITE);
fastButton = (Button) findViewById(R.id.fastlock);
fastButton.setBackgroundColor(Color.WHITE);
userButton = (Button) findViewById(R.id.userButton);
userButton.setBackgroundColor(Color.WHITE);
lockNum = (TextView) findViewById(R.id.lockNum);
unlockNum = (TextView) findViewById(R.id.unlockNum);
lockTime = (TextView) findViewById(R.id.howLongToLock);
DayOflock = (TextView) findViewById(R.id.DayOfLock);
ShowTimeTextView[0] = (TextView) findViewById(R.id.textView1);
ShowTimeTextView[1] = (TextView) findViewById(R.id.textView2);
ShowTimeTextView[2] = (TextView) findViewById(R.id.textView3);
ShowTimeTextView[3] = (TextView) findViewById(R.id.textView4);
ShowTimeTextView[4] = (TextView) findViewById(R.id.textView5);
}
/*
* 获取一直以来锁屏的次数、解锁次数、锁屏时长,并且更新到界面上
*/
public void SetLockAndUnlockAndLockTime() {
int LOCKNUM = gdfs.getLockNum();
int UNLOCKNUM = gdfs.getUnlockNum();
long LOCKTIME = gdfs.getLockTime_long();
lockNum.setText(LOCKNUM + "次");
unlockNum.setText(UNLOCKNUM + "次");
lockTime.setText(LOCKTIME + "分钟");
}
/*
* 从SP文件中获取定时锁屏的状态按钮的状态
*/
public void getModelStatus() {
// 获取开关的状态,如果上一次程序退出时候状态为true则进入程序时为true
status = gdfs.getStatus();
System.out.println(status);
if (status == true) {
getDayAndTimeForShow();
if ((timingToggleButton.isChecked() == false)
&& !gdfs.getDayOfWeek().equals("")
&& (!gdfs.getFiveTime().equals("")
|| !gdfs.getFourTime().equals("") || !gdfs
.getThreeTime().equals(""))
|| !gdfs.getTwoTime().equals("")
|| !gdfs.getOneTime().equals("")) {
timingToggleButton.toggle();
}
}
}
/*
* 设置点击事件 点击即进入设置日期、事件的designActivity
*/
public void setButtonOnListener() {
// 定时锁屏设置时间按钮添加点击事件
timingSetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this,
designActivity.class);
startActivity(intent);
finish();
}
});
/*
* 启动一键锁屏按钮设置点击事件
*/
timingToggleButton
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
// 点击之后自动调用的方法
@Override
public void onCheckedChanged(CompoundButton arg0,
boolean isChecked) {
// 判断按钮状态
if (isChecked) {
// 创建一个对话框
AlertDialog.Builder builder1 = new AlertDialog.Builder(
MainActivity.this);
builder1.setTitle("定时锁屏");// 设置对话框的标题
builder1.setMessage("你确定要启动定时锁屏吗?启动之前先设置定时锁屏的日期与时间哦!");// 设置对话框的内容
builder1.setPositiveButton("确定",
new OnClickListener() { // 这个是设置确定按钮
@Override
public void onClick(
DialogInterface arg0, int arg1) {
// 判断时间日期
if (!gdfs.getDayOfWeek().equals("")
&& (!gdfs.getFiveTime()
.equals("")
|| !gdfs.getFourTime()
.equals("") || !gdfs
.getThreeTime()
.equals(""))
|| !gdfs.getTwoTime()
.equals("")
|| !gdfs.getOneTime()
.equals("")) {
Intent i = new Intent(
MainActivity.this,
tixingActivity.class);
startActivity(i);
Toast.makeText(
MainActivity.this,
"定时锁屏启动成功",
Toast.LENGTH_SHORT)
.show();
gdfs.putStatusToTrue();
getDayAndTimeForShow();
} else {
timingToggleButton.toggle();
Toast.makeText(
MainActivity.this,
"启动失败,请先设置好时间和日期",
Toast.LENGTH_SHORT)
.show();
}
}
});
builder1.setNegativeButton("取消",
new OnClickListener() { // 取消按钮
@Override
public void onClick(
DialogInterface arg0, int arg1) {
// 如果点击了取消,将状态改为false;
timingToggleButton.toggle();
}
});
// 让对话框可见
builder1.create().show();
} else {
DayOflock.setText("锁屏日期:");
ShowTimeTextView[0].setText("时间段1:");
ShowTimeTextView[1].setText("时间段2:");
ShowTimeTextView[2].setText("时间段3:");
ShowTimeTextView[3].setText("时间段4:");
ShowTimeTextView[4].setText("时间段5:");
gdfs.putStatusToFalse();
}
}
});
fastButton.setOnClickListener(new View.OnClickListener() {
/*
* 设置单击事件进入一键锁屏的设置时间的fastActivity
*/
public void onClick(View arg0) {
Intent i = new Intent(MainActivity.this, fastActivity.class);
startActivity(i);
// finish();
}
});
userButton.setOnClickListener(new View.OnClickListener() {
/*
* 进入使用说明activity
*/
public void onClick(View arg0) {
Intent i = new Intent(MainActivity.this, explainActivity.class);
startActivity(i);
}
});
}
/*
* 从文件中获取时间和日期信息 设置将时间、日期显示在Activity中
*/
public void getDayAndTimeForShow() {
String Day = gdfs.getDayOfWeek();
DayOflock.setText("锁屏日期:" + Day);
String One = gdfs.getOneTime();
ShowTimeTextView[0].setText("时间段1:" + One);
String Two = gdfs.getTwoTime();
ShowTimeTextView[1].setText("时间段2:" + Two);
String Three = gdfs.getThreeTime();
ShowTimeTextView[2].setText("时间段3:" + Three);
String Four = gdfs.getFourTime();
ShowTimeTextView[3].setText("时间段4:" + Four);
String Five = gdfs.getFiveTime();
ShowTimeTextView[4].setText("时间段5:" + Five);
}
}
代码中的所有gdfs.方法都是自己封装的sp文件类定义的方法。sp文件的使用非常简单。为了方便大家读懂项目,这里我直接贴出自己封装的sp类的构造
public class getDataFromSp {
private SharedPreferences mySP; //共享存贮对象
private SharedPreferences.Editor mySPEditor;
private String ONE,TWO,THREE,FOUR,FIVE;
public getDataFromSp(Context context){mySP = context.getSharedPreferences("myLock",Context.MODE_PRIVATE);
mySPEditor = mySP.edit();
}
这样就完成了一个sp类的封装啦,构造SharedPreferences对象时需要用到当前的Context,所以我们在构造方法传进来一个。然后通过context.getSharedPreferences(文件名,文件的性质)设置文件,然后haredPreferences.Editor.edit()即可使用
举出一个例子
//从SP中获取锁屏次数
public int getLockNum(){
int num = mySP.getInt("lockNum",0);//获取一个Int类型的对象,对象名为lockNum,默认值为0 ----如果是其他类型活成getXXX即可
return num;
}
//将更新后的锁屏次数保存到SP中,每次调用方法锁屏次数加1
public void setLockNum(){
int num = mySP.getInt("lockNum",0);
num++;
mySPEditor.putInt("lockNum", num); //将num存到对象名为lockNum中,类型为int型
mySPEditor.commit();
}
源码下载 点击打开链接