EditText_5

activity_edit_jump.xml 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp" >

    <View
        android:layout_width="match_parent"
        android:layout_height="20dp" />

    <EditText
        android:id="@+id/et_username"
        style="@style/text_normal"
        android:hint="请输入用户名"
        android:inputType="text"
        android:background="@drawable/editext_selector" />

    <View
        android:layout_width="match_parent"
        android:layout_height="20dp" />
    
    <EditText
        android:id="@+id/et_password"
        style="@style/text_normal"
        android:hint="请输入密码"
        android:inputType="textPassword"
        android:background="@drawable/editext_selector" />
    
    <View
        android:layout_width="match_parent"
        android:layout_height="20dp" />

    <Button
        android:id="@+id/btn_login"
        style="@style/btn_relative"
        android:layout_width="match_parent"
        android:text="登录" />
    
</LinearLayout>
    <style name="text_normal">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:padding">3dp</item>
    </style>

    <style name="btn_relative">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:gravity">center</item>
        <item name="android:textColor">@color/black</item>
        <item name="android:textSize">17sp</item>
    </style>

editext_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@drawable/shape_edit_focus"/>
    <item android:drawable="@drawable/shape_edit_normal"/>
</selector>

 MainActivity.java

package com.example.third;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_jump);
        // 从布局文件中获取名叫et_username的用户名编辑框
        EditText et_username = findViewById(R.id.et_username);
        // 从布局文件中获取名叫et_password的密码编辑框
        EditText et_password = findViewById(R.id.et_password);
        Button btn_login = findViewById(R.id.btn_login);
        // 给用户名编辑框添加文本变化监听器
        et_username.addTextChangedListener(new JumpTextWatcher(et_username, et_password));
        // 给密码编辑框添加文本变化监听器
        et_password.addTextChangedListener(new JumpTextWatcher(et_password, btn_login));
        btn_login.setOnClickListener(this);
    }


    // 定义一个编辑框监听器,在输入回车符时自动跳到下一个控件
    private class JumpTextWatcher implements TextWatcher {
        private EditText mThisView;  // 声明当前的编辑框对象
        private View mNextView; // 声明下一个视图对象

        public JumpTextWatcher(EditText vThis, View vNext) {
            super();
            mThisView = vThis;
            if (vNext != null) {
                mNextView = vNext;
            }
        }

        // 在编辑框的输入文本变化前触发
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        // 在编辑框的输入文本变化时触发
        public void onTextChanged(CharSequence s, int start, int before, int count) {}

        // 在编辑框的输入文本变化后触发
        public void afterTextChanged(Editable s) {
            String str = s.toString();
            // 发现输入回车符或换行符
            if (str.contains("\r") || str.contains("\n")) {
                // 去掉回车符和换行符
                mThisView.setText(str.replace("\r", "").replace("\n", ""));
                if (mNextView != null) {
                    // 让下一个视图获得焦点,即将光标移到下个视图
                    mNextView.requestFocus();
                    // 如果下一个视图是编辑框,则将光标自动移到编辑框的文本末尾
                    if (mNextView instanceof EditText) {
                        EditText et = (EditText) mNextView;
                        // 让光标自动移到编辑框内部的文本末尾
                        // 方式一:直接调用EditText的setSelection方法
                        et.setSelection(et.getText().length());
                        // 方式二:调用Selection类的setSelection方法
                        //Editable edit = et.getText();
                        //Selection.setSelection(edit, edit.length());
                    }
                }
            }
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_login) {
            Toast.makeText(this, "这个登录按钮啥事也没做", Toast.LENGTH_SHORT).show();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值