Android按钮及按钮处理事件

MainActivity.java代码

package com.example.hello;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView txt;
    private TextView zt;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (TextView)findViewById(R.id.pwd1);
        zt = (TextView)findViewById(R.id.zt1);
        button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new mClick());
    }

    class mClick implements View.OnClickListener {
        public void onClick(View view){
            String pwds;
            pwds = txt.getText().toString();
            if(pwds.equals("123"))
                zt.setText("登录成功");
            else
                zt.setText("登录失败");
        }
    }
}
View Code

activity_main.xml代码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout android:layout_height="match_parent"
 3     android:layout_width="match_parent"
 4     android:orientation="vertical"
 5     xmlns:android="http://schemas.android.com/apk/res/android">
 6 <!--    <TextView
 7         android:id="@+id/txt1"
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content"
10         android:text="@string/hello"
11         android:textColor="#0ff"
12         android:gravity="center"
13         />-->
14     <EditText
15         android:id="@+id/acnt1"
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:hint="请输入账户" />
19     <EditText
20         android:id="@+id/pwd1"
21         android:layout_width="match_parent"
22         android:layout_height="wrap_content"
23         android:hint="请输入密码"
24         android:inputType="textPassword"/>
25     <Button
26         android:id="@+id/button1"
27         android:layout_width="wrap_content"
28         android:layout_height="wrap_content"
29         android:text="登录"
30         android:layout_gravity="center" />
31     <TextView
32         android:id="@+id/zt1"
33         android:layout_width="match_parent"
34         android:layout_height="wrap_content"
35         android:text="登录状态:无"/>
36 </LinearLayout>
View Code

程序样式

原文地址:https://www.cnblogs.com/xiaowangdatie/p/13720948.html