参考视频:Android Development for Beginners - Full Course
一、MainActivity.java
package com.example.challenge_1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Register(View view)
{
TextView textView_display = findViewById(R.id.TextView_Display);
EditText edtText_uname = findViewById(R.id.EdtText_Username);
EditText editText_pwd = findViewById(R.id.EdtText_Password);
textView_display.setText("Uname: " + edtText_uname.getText().toString() + ", Pwd: " + editText_pwd.getText().toString());
}
}
一、activity_main.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=".MainActivity">
<!-- Username -->
<EditText
android:id="@+id/EdtText_Username"
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_marginTop="100dp"
android:ems="10"
android:hint="Username"
android:inputType="text"
android:textSize="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<!-- Password -->
<EditText
android:id="@+id/EdtText_Password"
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_marginTop="200dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:textSize="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<!-- Register BTN -->
<Button
android:id="@+id/Btn_Register"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_marginTop="400dp"
android:text="Register"
android:textSize="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick="Register"
/>
<!-- TextView -->
<TextView
android:id="@+id/TextView_Display"
android:layout_width="500dp"
android:layout_height="100dp"
android:layout_marginTop="600dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>