Android自學之路5

EditText&簡單登錄頁面的製作
*常用屬性
*監聽事件
*製作登錄界面
EditTextActivity

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EditTextActivity extends AppCompatActivity {

    private EditText username;
    private Button mBtnLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_text);
        mBtnLogin=findViewById(R.id.btn_login);
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(EditTextActivity.this,"登陸成功!",Toast.LENGTH_SHORT).show();

            }
        });
        username =findViewById(R.id.ed_1);
        username.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.d("editText",s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}

Main部分

  mBtnEditText =findViewById(R.id.btn_edittext);
        mBtnEditText.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             Intent intent = new Intent(MainActivity.this, EditTextActivity.class);
             startActivity(intent);
         }
     });

xml代碼爲

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp">
<EditText
    android:id="@+id/ed_1"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:textSize="16sp"
    android:textColor="#FFAD33"
    android:hint="用戶名"
    android:background="@drawable/bg_username"
    android:padding="10dp"

    />
<EditText
    android:id="@+id/ed_2"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:textSize="16sp"
    android:layout_marginTop="15dp"
    android:textColor="#FFAD33"
    android:hint="密碼"
    android:inputType="textPassword"
    android:layout_below="@id/ed_1"
    android:padding="10dp"
    android:background="@drawable/bg_username"
    />
    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/ed_2"
        android:layout_marginTop="40dp"
        android:text="登錄"
        android:background="@drawable/bg_btn"
        android:textColor="#fff"
        android:textSize="20sp"
        />

</RelativeLayout>

效果如圖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章