10天冲刺第七天
今天做了什么:
<?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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".submissionActivity"> <ScrollView android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="1" tools:ignore="MissingConstraints"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="20dp" android:background="@color/white" /> <EditText android:id="@+id/ed_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/shape_gray_square_bg" android:hint="标题" android:maxLength="20" android:textSize="30dp"/> <EditText android:id="@+id/ed_context" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/shape_gray_square_bg" android:gravity="top" android:minHeight="500dp" android:maxLength="1500" android:hint="正文" /> <Button android:id="@+id/bt_commit" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="投稿"/> </LinearLayout> </ScrollView> </LinearLayout>
对应代码:
package com.example.psychological; import android.content.Intent; import android.os.Bundle; import android.os.Looper; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import com.example.psychological.javaClass.Dao; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Date; public class submissionActivity extends AppCompatActivity implements View.OnClickListener { private EditText ed_title; private EditText ed_context; private Button bt_commit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_submission); getSupportActionBar().hide(); ed_title = findViewById(R.id.ed_title); ed_context = findViewById(R.id.ed_context); bt_commit = findViewById(R.id.bt_commit); bt_commit.setOnClickListener(this); } @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { Dao d= new Dao(); String title=ed_title.getText().toString(); String context=ed_context.getText().toString(); if(title.equals("")||context.equals("")){ Looper.prepare(); Toast.makeText(submissionActivity.this,"输入为空", Toast.LENGTH_SHORT).show(); Looper.loop(); } SimpleDateFormat formatter= new SimpleDateFormat("yyyy.MM.dd"); Date date = new Date(System.currentTimeMillis()); try { d.addEssay(String.valueOf(d.getLength2()),title,context,formatter.format(date)); } catch (SQLException e) { throw new RuntimeException(e); } finish(); startActivity(new Intent(submissionActivity.this,MainActivity.class)); } }).start(); } }
文章展示界面:
<?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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".essayActivity"> <ScrollView android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="1" tools:ignore="MissingConstraints"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="20dp" android:background="@color/white"/> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30dp" android:gravity="center"/> <TextView android:id="@+id/tv_context" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </ScrollView> </LinearLayout>
资源文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="2dp" android:background="@color/black"/> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30dp" android:background="@color/teal_200" android:minLines="2" /> <TextView android:id="@+id/tv_date" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/gray" android:gravity="right" android:minLines="1" android:maxLines="1" /> <ImageView android:layout_width="match_parent" android:layout_height="20dp" android:background="@color/white"/> </LinearLayout> </LinearLayout>
对应代码:
package com.example.psychological; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.TextView; import androidx.activity.EdgeToEdge; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import com.example.psychological.javaClass.Dao; import com.example.psychological.javaClass.Essay; import java.sql.SQLException; public class essayActivity extends AppCompatActivity { private TextView tv_title; private TextView tv_context; private Essay essay; private Handler handler=new Handler(new Handler.Callback() { @Override public boolean handleMessage(@NonNull Message msg) { switch (msg.what){ case 0: tv_title.setText(essay.getTitle()); tv_context.setText(essay.getContext()); break; } return true; } }); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_essay); getSupportActionBar().hide(); tv_title=findViewById(R.id.tv_title); tv_context=findViewById(R.id.tv_context); initData(); } public void initData(){ new Thread(new Runnable() { @Override public void run() { Dao d=new Dao(); Intent intent=getIntent(); String num=intent.getStringExtra("num"); try { essay=d.checkEssay(num); } catch (SQLException e) { throw new RuntimeException(e); } Message msg=new Message(); msg.what=0; handler.sendMessage(msg); } }).start(); } }
明天做什么:
预约后通知功能
遇到的问题:输入框随输入拉长,设置初始高度