在本周我接触了安卓的基础学习,并且通过自学完成了一个每日打卡app
每日打卡app源码
alarmActivity,java
package com.example.myapp01; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class alarmActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alarm); } }
Demo01.java
package com.example.myapp01; import androidx.appcompat.app.AppCompatActivity; import java.text.SimpleDateFormat; import java.util.Date; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Demo01 extends AppCompatActivity { private Button Daka2; private EditText DakaContent; private MYsqliteopenhelper mYsqliteopenhelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo01); mYsqliteopenhelper = new MYsqliteopenhelper(this); find(); } public void find(){ Daka2 = findViewById(R.id.Daka2); DakaContent = findViewById(R.id.DaKaIn); } public void Daydaka(View view){ String s = DakaContent.getText().toString(); System.out.println(s); long l = mYsqliteopenhelper.DakeIn(s); if(l!=-1){ Toast.makeText(this,"打卡成功",Toast.LENGTH_SHORT).show(); Intent i3 = new Intent(this,good.class); startActivity(i3); }else{ Toast.makeText(this,"打卡失败",Toast.LENGTH_SHORT).show(); } } }
demo02.java
package com.example.myapp01; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class Demo02 extends AppCompatActivity { private Button chaxun; private TextView showcontent,showdate; private MYsqliteopenhelper mYsqliteopenhelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo02); mYsqliteopenhelper = new MYsqliteopenhelper(this); find(); } public void find(){ chaxun = findViewById(R.id.chaxun); showcontent = findViewById(R.id.tv_showcontent); showdate = findViewById(R.id.tv_showdate); } public void chaxun(View view){ SQLiteDatabase db = mYsqliteopenhelper.get(); showcontent.setText("打卡内容"); showdate.setText("日期"); Cursor cursor = db.rawQuery("select * from note",null);//查询所有数据 while (cursor.moveToNext()) { @SuppressLint("Range") String content = cursor.getString(cursor.getColumnIndex("content"));//查询数据name @SuppressLint("Range") String date = cursor.getString(cursor.getColumnIndex("date"));//查询数据age showcontent.setText(showcontent.getText() + "\n" + content);//在一个textView显示所有查询到的数据一条加一个换号 showdate.setText(showdate.getText()+"\n" + date); } cursor.close(); } }
good.java
package com.example.myapp01; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class good extends AppCompatActivity implements View.OnClickListener{ private Button DaKa,ChaXun,NaoZhong; private MYsqliteopenhelper mYsqliteopenhelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_good); mYsqliteopenhelper = new MYsqliteopenhelper(this); find(); } private void find(){ DaKa = findViewById(R.id.DaKa); DaKa.setOnClickListener(this); ChaXun = findViewById(R.id.chaxun); ChaXun.setOnClickListener(this); NaoZhong = findViewById(R.id.naozhong); NaoZhong.setOnClickListener(this); } public void onClick(View view){ int id = view.getId(); switch (id){ case R.id.DaKa: Intent i =new Intent(this,Demo01.class); startActivity(i); break; case R.id.chaxun: Intent j = new Intent(this,Demo02.class); startActivity(j); break; case R.id.naozhong: Intent k = new Intent(this,TimeClock.class); startActivity(k); break; } } }
MainActivity.java
package com.example.myapp01; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.example.myapp01.javabean.User; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button login,register; //从xml中获得的两个按钮 登录和注册 private EditText name,password; //从xml中获得的两个数据 name 和 password private MYsqliteopenhelper mYsqliteopenhelper; //连接数据的对象 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mYsqliteopenhelper = new MYsqliteopenhelper(this);//创建功能类执行其中的登录和注册功能 find(); } private void find(){ //获得从xml中设置的各种按钮和数据 login = findViewById(R.id.login); register = findViewById(R.id.register); name = findViewById(R.id.edname); password = findViewById(R.id.edpassword); login.setOnClickListener(this); register.setOnClickListener(this); } @Override public void onClick(View view) { int id = view.getId(); switch (id){ //来分别设置两个按钮的功能 case R.id.login: String s = name.getText().toString(); //获得xml中获得name值 String s1 = password.getText().toString(); //获得xml中获得的password值v boolean login = mYsqliteopenhelper.login(s,s1); //调用MYsqliteopenhelper中写的登录功能 if(login){ Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show(); //给出登陆成功提示 Intent i = new Intent(this,good.class); //设置跳转 startActivity(i); //完成跳转 }else{ Toast.makeText(this,"登录失败",Toast.LENGTH_SHORT).show(); //给出登陆失败提示 } break; case R.id.register: Intent i1 = new Intent(this, com.example.myapp01.register.class);//直接跳转到注册界面 startActivity(i1); break; } } }
MYsqliteopenhelper.java
package com.example.myapp01; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import java.text.SimpleDateFormat; import androidx.annotation.Nullable; import com.example.myapp01.javabean.User; import java.sql.SQLInput; import java.util.Date; public class MYsqliteopenhelper extends SQLiteOpenHelper { private static final String DB_NAME="db3"; public static final String TABLE_NAME_NOTES = "note"; public static final String COLUMN_NAME_ID = "_id"; public static final String COLUMN_NAME_NOTE_CONTENT = "content"; public static final String COLUMN_NAME_NOTE_DATE = "date"; private static final String create_users = "create table users(name varchar(32),password varchar(32))";//sql语句 private static final String create_note = "CREATE TABLE " + TABLE_NAME_NOTES + "(" + COLUMN_NAME_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NAME_NOTE_CONTENT + " TEXT NOT NULL DEFAULT\"\"," + COLUMN_NAME_NOTE_DATE + " TEXT NOT NULL DEFAULT\"\"" + ")"; public MYsqliteopenhelper(@Nullable Context context) { super(context, DB_NAME, null, 1); } //与数据库进行连接的具体代码 @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL(create_users); sqLiteDatabase.execSQL(create_note); }//调用sql语句来创建表 @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } public long register(User u){ //注册功能的实现 数据库方面 SQLiteDatabase db = getWritableDatabase();//获得数据库连接 ContentValues cv = new ContentValues(); //大概是个暂时储存数据的一个容器 cv.put("name",u.getName()); //将name存进cv中 cv.put("password",u.getPassword()); //将password存进cv中 long users = db.insert("users",null,cv); //从cv中取出数据并且放入数据库中 return users; //返回一个值 有值说明取出成功 否则失败 用于判断 } public boolean login(String name,String password){ //登录功能的实现 数据库方面 SQLiteDatabase db1 = getWritableDatabase(); //获得数据库的连接 boolean result = false; //创建一个boolean类型 以判断是否登录成功 Cursor users = db1.query("users",null,"name like ?",new String[]{name},null,null,null); //获得账号的哪一行数据 用users存储 if(users != null){ //如果 users不为空 就进行密码校对 否则直接返回false while(users.moveToNext()){ //进行密码校对 String password1 = users.getString(1); result = password1.equals(password); return result; } } return false; } public long DakeIn(String content){ SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); Date curDate = new Date(System.currentTimeMillis());//获取当前时间 String str = formatter.format(curDate); SQLiteDatabase db = getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put("content",content); cv.put("date",str); long flag = db.insert("note",null,cv); System.out.println(flag); return flag; } public SQLiteDatabase get(){ SQLiteDatabase db = getWritableDatabase(); return db; } }
register.java
package com.example.myapp01; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.example.myapp01.javabean.User; public class register extends AppCompatActivity { private Button register1; private EditText name1,password1; private MYsqliteopenhelper mYsqliteopenhelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); mYsqliteopenhelper = new MYsqliteopenhelper(this); find(); } private void find(){ register1 = findViewById(R.id.register1); name1 = findViewById(R.id.edname1); password1 = findViewById(R.id.edpassword1); } public void zhuce(View view){ String s = name1.getText().toString(); String s1 = password1.getText().toString(); User u =new User(s,s1); long l = mYsqliteopenhelper.register(u); if(l!=-1){ Toast.makeText(this,"注册成功",Toast.LENGTH_SHORT).show(); Intent i3 = new Intent(this,MainActivity.class); startActivity(i3); }else{ Toast.makeText(this,"注册失败",Toast.LENGTH_SHORT).show(); } } }
TimeClock.java
package com.example.myapp01; import androidx.appcompat.app.AppCompatActivity; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TimePicker; import android.widget.Toast; import java.util.Calendar; public class TimeClock extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_time_clock); final TimePicker timePicker=findViewById(R.id.time); //获取时间拾取组件 Button button=findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { //给'设置闹钟'按钮设置监听 @Override public void onClick(View v) { Intent intent=new Intent(TimeClock.this,alarmActivity.class); PendingIntent pend=PendingIntent.getActivity(TimeClock.this,0,intent,0); //显示闹钟,alarmActivity AlarmManager alarm= (AlarmManager) getSystemService(Context.ALARM_SERVICE); // 通过Context.ALARM_SERVICE获取AlarmManager对象 Calendar calendar =Calendar.getInstance(); //获取日历对象 calendar.set(Calendar.HOUR_OF_DAY,timePicker.getHour()); //利用时间拾取组件timePicker得到要设定的时间 calendar.set(Calendar.MINUTE,timePicker.getMinute()); calendar.set(Calendar.SECOND,0); alarm.set(AlarmManager.RTC,calendar.getTimeInMillis(),pend); //设定闹钟 //AlarmManager.ELAPSED_REALTIME 在指定延迟后提醒 //AlarmManager.ELAPSED_REALTIME_WAKEUP 在指定延迟后提醒,并唤醒系统 //AlarmManager.RTC 在指定时间提醒 //AlarmManager.RTC_WAKEUP 在指定时间提醒并唤醒系统 } }); } }
activity_alarm.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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=".alarmActivity"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteX="0dp" tools:layout_editor_absoluteY="0dp" /> </androidx.constraintlayout.widget.ConstraintLayout>
activity_demo01
<?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=".Demo01" android:orientation="vertical"> <TextView android:layout_width="400dp" android:layout_height="200dp" android:layout_weight="2" android:gravity="center" android:text="打卡内容" android:textSize="40dp" /> <EditText android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" android:layout_weight="4" android:id="@+id/DaKaIn" /> <Button android:id="@+id/Daka2" android:layout_width="160dp" android:layout_height="100dp" android:layout_gravity="center" android:text="打卡" android:textSize="27dp" android:onClick="Daydaka" /> </LinearLayout>
标签:每周,--,void,第一周,id,Intent,import,android,public From: https://www.cnblogs.com/sxwgzx23/p/17469883.html