Activity_ZSButton.xml
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入用户名:"
android:textSize="30sp"
android:textColor="#030303"
android:textStyle="bold"
android:layout_marginTop="10dp"/>
<EditText
android:id="@+id/editText_inputname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名:"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入密码:"
android:textSize="30sp"
android:textColor="#030303"
android:textStyle="bold"
android:layout_marginTop="10dp"/>
<EditText
android:id="@+id/editText_inputpwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密 码:"
android:inputType="textPassword"
android:textStyle="bold" />
<Button
android:id="@+id/button_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="30sp"
android:textColor="#000000"
android:textStyle="bold"
android:layout_marginTop="10dp" />
<Button
android:id="@+id/button_cancle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消"
android:textSize="30sp"
android:textColor="#000000"
android:textStyle="bold"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textView_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示获取的用户名和密码"
android:textSize="20sp"
android:textColor="#0E0D0E"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:visibility="gone"/>
</LinearLayout>
ZSButtonActivity.java
package com.example.zsbutton;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText edit_name,edit_pwd;
private Button btn_login,btn_cancle;
private TextView txt_display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit_name=findViewById(R.id.editText_inputname);
edit_pwd=findViewById(R.id.editText_inputpwd);
btn_login = findViewById(R.id.button_login);
btn_cancle = findViewById(R.id.button_cancle);
txt_display = findViewById(R.id.textView_display);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String strname = edit_name.getText().toString();
String strpwd = edit_pwd.getText().toString();
txt_display.setVisibility(View.VISIBLE);
txt_display.setText("哈哈,我偷偷地盗取了你的秘密信息\\n用户名为:\"+strname + \"\\n 密码为:\" +strpwd");
}
});
btn_cancle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txt_display.setVisibility(View.GONE);
edit_name.setText("");
edit_pwd.setText("");
}
});
}
}