本次项目主要是结课的一个关于Android的基本应用开发,根据所学习到的知识,开发一个简单的入门级应用。
PC计算机(操作系统win10)
JDK安装包
Android studio 3.5.2
Android SDK 集成环境
如图所示,APP包含三个页面,启动页(activity_welcome)、主页面(activity_main)和添加页面(activity_add)
如图所示,APP的布局文件均存放在layout文件夹中、所调用函数和方法均存放于com.example.bwl中。所需要的图片均存放在drawable。
启动Android studio,创建新的项目 这里我们命名为BWL。步骤如图4、5、6所示。语言选择Java,最后单击Finish完成创建。
将左上角的Android切换成Project,找到com.example.bwl,在同一目录下创建一个新的activity,创建方法与创建项目方法相同。步骤如下图。将新创建的activity项命名为welcome。
<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=".WelcomeActivity"
android:background="@drawable/bcg"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:textSize="20sp" />
</RelativeLayout>
</LinearLayout>
配置启动顺序,打开AndroidMainfest.xml文件,如图所示,将intent代码块放在WelcomeActivity里
import java.util.Timer;
import android.content.Intent;
import java.util.TimerTask;
public class WelcomeActivity extends AppCompatActivity implements View.OnClickListener {
private int recLen = *;//跳过倒计时提示*秒
private TextView tv;
Timer timer = new Timer();
private Handler handler;
private Runnable runnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//定义全屏参数
int flag= WindowManager.LayoutParams.FLAG_FULLSCREEN;
//设置当前窗体为全屏显示
getWindow().setFlags(flag, flag);
setContentView(R.layout.activity_welcome);
initView();
timer.schedule(task, 1000, 1000);//等待时间一秒,停顿时间一秒
/**
* 不点击跳过
*/
handler = new Handler();
handler.postDelayed(runnable = new Runnable() {
@Override
public void run() {
//从闪屏界面跳转到主界面
Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}, ****);//设置延迟时间,后发送handler信息
}
private void initView() {
tv = findViewById(R.id.tv);//跳过
tv.setOnClickListener(this);//跳过监听
}
TimerTask task = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() { // UI thread
@Override
public void run() {
recLen--;
tv.setText("跳过 " + recLen);
if (recLen < 0) {
timer.cancel();
tv.setVisibility(View.GONE);//倒计时到0隐藏字体
}
}
});
}
};
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv:
//跳转到主界面
Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
startActivity(intent);
finish();
if (runnable != null) {
handler.removeCallbacks(runnable);
}
break;
default:
break;
}
}
}
<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:background="@drawable/backgroundone"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="我的备忘录"
android:textColor="#474646"
android:textSize="45sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<EditText
android:id="@+id/view_select"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ems="13"
android:hint="此处可以查询数据!"
android:inputType="textPersonName" />
<ImageButton
android:id="@+id/btn_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
app:srcCompat="@android:drawable/ic_menu_search" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="490dp"
android:layout_margin="5dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/data"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:textSize="20dp"></TextView>
<Button
android:id="@+id/next"
android:layout_width="206dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:onClick="onClickNext"
android:text="添加新备忘"
tools:ignore="DuplicateIds" />
</LinearLayout>
</ScrollView>
<ImageButton
android:id="@+id/btn_add"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="300dp"
android:background="@android:color/transparent"
app:srcCompat="@mipmap/add1" />
</LinearLayout>
<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=".add">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/btn_back"
android:layout_width="120dp"
android:layout_height="60dp"
android:background="@drawable/colorsilve"
app:srcCompat="@android:drawable/ic_menu_revert" />
<ImageButton
android:id="@+id/btn_finish"
android:layout_width="120dp"
android:layout_height="60dp"
android:visibility="visible"
android:background="@drawable/colorsilve"
app:srcCompat="@android:drawable/checkbox_on_background" />
<ImageButton
android:id="@+id/btn_clear"
android:layout_width="120dp"
android:layout_height="60dp"
android:visibility="visible"
android:background="@drawable/colorsilve"
app:srcCompat="@android:drawable/ic_delete" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/now_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textStyle="bold"
android:textSize="20dp">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/data_information"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFFFF"
android:gravity="start"
android:textSize="25dp"
android:hint="此处写文章"
android:textColor="#070507"
>
</EditText>
</LinearLayout>
</LinearLayout>
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ImageButton btn_add;
private TextView data;
private ImageButton btn_select;
private EditText view_select;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = findViewById(R.id.data);
btn_select = findViewById(R.id.btn_select);
view_select = findViewById(R.id.view_select);
//跳转
btn_add = (ImageButton) findViewById(R.id.btn_add);
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, add.class);
startActivity(intent);
}
});
Intent i = getIntent();
String get_data = i.getStringExtra("data");
data.setText(get_data);
//查询监听
btn_select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String input = view_select.getText().toString().trim();
String get = data.getText().toString().trim();
if (!input.isEmpty()) {
int a = get.indexOf(input); //内容第一次出现
if ( a != -1 ){
String front = get.substring(0,a+1);
String after = get.substring(a+1,get.length());
int a1 = front.lastIndexOf("2020");
int b1 = after.indexOf("2020");
String front2 ;//传值
String after2 ;//传值
//防止查询数据为第一
if (a1 == -1) {
front2 = front;
}else{
String front1 = front.substring(a1, front.length());
front2 = front1;
}
if (b1 == -1){
after2 = after;
}else {
String after1 = after.substring(0,b1);
after2 = after1;
}
showMsg(front2 + after2 );//显示查询结果信息
}else {
showMsg("无匹配数据");
}
}else{
showMsg("请输入查询的数据");
}
}
});
}
//显示提示
private void showMsg(String msg){
Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
}
public void onClickNext(View view) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, add.class);//this前面为当前activty名称,class前面为要跳转到得activity名称
startActivity(intent);
}
}
public class add extends AppCompatActivity {
private ImageButton btn_back; //返回
private ImageButton btn_finish;//完成
private ImageButton btn_clear;//清空
private TextView now_time;//当前时间
private EditText data_information;//存储数据
//与数据库操作相关的成员变量
private MyDbHelper myDbHelper;
private SQLiteDatabase db;
private ContentValues values;
private static final String mTablename = "mymemo";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
//建立联系
btn_back = findViewById(R.id.btn_back);
data_information = findViewById(R.id.data_information);
now_time = findViewById(R.id.now_time);
btn_finish = findViewById(R.id.btn_finish);
btn_clear = findViewById(R.id.btn_clear);
//初始化数据库工具类实例
myDbHelper = new MyDbHelper(this);
//获取手机当前时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
final String str_time = simpleDateFormat.format(date);
now_time.setText(str_time);
//返回主界面
btn_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(add.this,MainActivity.class);
startActivity(intent);
queryData();
}
});
//执行添加
btn_finish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String input = data_information.getText().toString().trim();
if (input.isEmpty()){
showMsg("请输入要记录的内容");
}else {
addData(str_time.trim(),input);
finish();
queryData();
}
}
});
btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearData();
showMsg("数据清楚完毕");
}
});
}
private void addData(String now_time,String data_information){
db = myDbHelper.getWritableDatabase();
values = new ContentValues();
values.put("now_time",now_time);
values.put("information",data_information);
db.insert(mTablename,null,values);
showMsg("添加成功");
}
//清空数据
private void clearData(){
db = myDbHelper.getWritableDatabase();
db.delete(mTablename,null,null);
String srtResult = "";
Intent intent = new Intent(add.this,MainActivity.class);
intent.putExtra("data",srtResult);
startActivity(intent);
showMsg("已经全部清空");
}
//显示消息
private void showMsg(String msg){
Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
}
//查询方法
private void queryData(){
db = myDbHelper.getReadableDatabase();
Cursor cursor = db.query(mTablename,null,null,null,
null,null,null);
String srtResult = "";
while (cursor.moveToNext()){
srtResult += "\n" + cursor.getString(1);
srtResult += "\n内容:" + cursor.getString(2);
srtResult += "\n";
}
Intent intent = new Intent(add.this,MainActivity.class);
intent.putExtra("data",srtResult);
startActivity(intent);
}
class MyDbHelper extends SQLiteOpenHelper{
public MyDbHelper(@Nullable Context context) {
super(context, "mymemo.db", null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + mTablename + "(_id integer primary key autoincrement, " +
"now_time varchar(50) unique,information varchar(100) )");
}
代码一定要多敲多练!代码一定要多敲多练!代码一定要多敲多练!
往期博客: https://blog.csdn.net/weixin_44087406/article/details/111187687