Android Studio中仅通过TextView展示日期:无需按钮及点击事件
Hey there! Let me walk you through exactly how to show a date using just a TextView—no buttons, no click events, just straight-up text display. It's simpler than you think, here's the step-by-step breakdown:
Step 1: Add TextView to Your Layout
First, open your layout file (usually activity_main.xml) and add a TextView element. Give it an ID so we can reference it in your Activity later, and tweak basic styling if you want:
<?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:gravity="center" android:orientation="vertical"> <!-- TextView for displaying the date --> <TextView android:id="@+id/dateTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#000000" /> </LinearLayout>
Step 2: Fetch and Format the Date in Your Activity
Next, open your corresponding Activity class (like MainActivity.java or MainActivity.kt). We'll grab the current date, format it into a readable string, and set it directly to the TextView.
For Java:
You can use the modern Java 8+ date/time APIs (recommended for reliability) or the legacy SimpleDateFormat. Here's the modern approach:
import android.os.Bundle; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get reference to the TextView TextView dateTextView = findViewById(R.id.dateTextView); // Define your desired date format DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // Get and format the current date/time String currentDate = LocalDateTime.now().format(formatter); // Set the formatted date to the TextView dateTextView.setText(currentDate); } }
Note: If you need to support devices below Android 8.0 (API level 26), add this to your module-level build.gradle to enable Java 8 features:
android { compileOptions { coreLibraryDesugaringEnabled true sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.4' }
For Kotlin:
Kotlin simplifies this even more with the same modern APIs:
import android.os.Bundle import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import java.time.LocalDateTime import java.time.format.DateTimeFormatter class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val dateTextView = findViewById<TextView>(R.id.dateTextView) val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") val currentDate = LocalDateTime.now().format(formatter) dateTextView.text = currentDate } }
Customize the Date Format
You can adjust the format string to display the date exactly how you want. Here are some common examples:
MMM dd, yyyy→ Oct 05, 2024EEEE, MMMM dd, yyyy→ Saturday, October 05, 2024dd/MM/yyyy→ 05/10/2024
Just swap the pattern in DateTimeFormatter.ofPattern() with your preferred format.
That's all! When you run your app, the TextView will immediately show the formatted current date without any extra interactions.
内容的提问来源于stack exchange,提问作者Armando Alba

