2024/3/22
所花时间:4小时
代码行:200行
博客量:1篇
了解到的知识点:进行安卓第一次作业得最后一个页面的编写
package com.example.enroll;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class statisticanalysis extends AppCompatActivity {
private Button mButton_number;
private Button mButton_put_days;
private TextView mTextView_goal_number;
private TextView mTextView_days;
public MySQLConnector mySQLConnector=new MySQLConnector();
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_statisticanalysis);
mTextView_goal_number=findViewById(R.id.TextView_goal_number);
mTextView_days=findViewById(R.id.TextView_days);
mButton_number=findViewById(R.id.btn_number);
mButton_put_days=findViewById(R.id.btn_put_days);
mButton_number.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
try{
Statement statement = mySQLConnector.getConn().createStatement();
String sql = "SELECT Count(*) FROM goal WHERE MONTH(mdate) = 3";
ResultSet rs = statement.executeQuery(sql);
int count1 = 0;
if (rs.next()) {
count1=rs.getInt("count(*)");
}
String str_count1=String.valueOf(count1);
Log.e("TAG",str_count1);
mTextView_goal_number.setText(str_count1);
// 3月份的数据条数存储在 count 变量中,可以在后续代码中使用
}catch (Exception e){
}
}
}).start();
}
});
mButton_put_days.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
try{
Statement statement = mySQLConnector.getConn().createStatement();
String sql = "SELECT Count(*) FROM learningrecord WHERE MONTH(enddate) = 3";
ResultSet rs = statement.executeQuery(sql);
int count = 0;
if (rs.next()) {
count =rs.getInt("Count(*)");
}
String str_count=String.valueOf(count);
Log.e("TAG",str_count);
mTextView_days.setText(str_count);
// 3月份的数据条数存储在 count 变量中,可以在后续代码中使用
}catch (Exception e){
}
}
}).start();
}
});
}
}
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class statisticanalysis extends AppCompatActivity {
private Button mButton_number;
private Button mButton_put_days;
private TextView mTextView_goal_number;
private TextView mTextView_days;
public MySQLConnector mySQLConnector=new MySQLConnector();
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_statisticanalysis);
mTextView_goal_number=findViewById(R.id.TextView_goal_number);
mTextView_days=findViewById(R.id.TextView_days);
mButton_number=findViewById(R.id.btn_number);
mButton_put_days=findViewById(R.id.btn_put_days);
mButton_number.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
try{
Statement statement = mySQLConnector.getConn().createStatement();
String sql = "SELECT Count(*) FROM goal WHERE MONTH(mdate) = 3";
ResultSet rs = statement.executeQuery(sql);
int count1 = 0;
if (rs.next()) {
count1=rs.getInt("count(*)");
}
String str_count1=String.valueOf(count1);
Log.e("TAG",str_count1);
mTextView_goal_number.setText(str_count1);
// 3月份的数据条数存储在 count 变量中,可以在后续代码中使用
}catch (Exception e){
}
}
}).start();
}
});
mButton_put_days.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
try{
Statement statement = mySQLConnector.getConn().createStatement();
String sql = "SELECT Count(*) FROM learningrecord WHERE MONTH(enddate) = 3";
ResultSet rs = statement.executeQuery(sql);
int count = 0;
if (rs.next()) {
count =rs.getInt("Count(*)");
}
String str_count=String.valueOf(count);
Log.e("TAG",str_count);
mTextView_days.setText(str_count);
// 3月份的数据条数存储在 count 变量中,可以在后续代码中使用
}catch (Exception e){
}
}
}).start();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".statisticanalysis"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginTop="20dp"
android:gravity="center"
tools:ignore="UselessParent">
<TextView
android:layout_width="400dp"
android:layout_height="match_parent"
android:text="@string/statistic_analysis"
android:textSize="40sp"
android:gravity="center"
android:textColor="#000000"
android:textStyle="bold">
</TextView>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="60dp"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp">
<Button
android:id="@+id/btn_number"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="@string/goal_number"
android:textSize="25sp"
android:textColor="#000000"
android:textStyle="bold">
</Button>
<TextView
android:id="@+id/TextView_goal_number"
android:layout_width="match_parent"
android:layout_height="60dp"
android:textSize="30sp"
android:layout_toEndOf="@id/btn_number"
android:gravity="center">
</TextView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="60dp"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp">
<TextView
android:id="@+id/text_2"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="@string/average_rate"
android:textSize="25sp"
android:textColor="#000000"
android:textStyle="bold">
</TextView>
<EditText
android:id="@+id/Edit_average_rate"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_toEndOf="@id/text_2">
</EditText>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="60dp"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp">
<Button
android:id="@+id/btn_put_days"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="@string/days"
android:textSize="25sp"
android:textColor="#000000"
android:textStyle="bold">
</Button>
<TextView
android:id="@+id/TextView_days"
android:layout_width="match_parent"
android:layout_height="60dp"
android:textSize="30sp"
android:layout_toEndOf="@id/btn_put_days"
android:gravity="center">
</TextView>
</RelativeLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".statisticanalysis"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginTop="20dp"
android:gravity="center"
tools:ignore="UselessParent">
<TextView
android:layout_width="400dp"
android:layout_height="match_parent"
android:text="@string/statistic_analysis"
android:textSize="40sp"
android:gravity="center"
android:textColor="#000000"
android:textStyle="bold">
</TextView>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="60dp"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp">
<Button
android:id="@+id/btn_number"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="@string/goal_number"
android:textSize="25sp"
android:textColor="#000000"
android:textStyle="bold">
</Button>
<TextView
android:id="@+id/TextView_goal_number"
android:layout_width="match_parent"
android:layout_height="60dp"
android:textSize="30sp"
android:layout_toEndOf="@id/btn_number"
android:gravity="center">
</TextView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="60dp"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp">
<TextView
android:id="@+id/text_2"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="@string/average_rate"
android:textSize="25sp"
android:textColor="#000000"
android:textStyle="bold">
</TextView>
<EditText
android:id="@+id/Edit_average_rate"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_toEndOf="@id/text_2">
</EditText>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="60dp"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp">
<Button
android:id="@+id/btn_put_days"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="@string/days"
android:textSize="25sp"
android:textColor="#000000"
android:textStyle="bold">
</Button>
<TextView
android:id="@+id/TextView_days"
android:layout_width="match_parent"
android:layout_height="60dp"
android:textSize="30sp"
android:layout_toEndOf="@id/btn_put_days"
android:gravity="center">
</TextView>
</RelativeLayout>
</LinearLayout>