android:实时监听键盘表单输入的值。

在这里插入图片描述

layout/activity_jinapanjianting.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:text="Please input the text:"
        />
    <EditText android:id="@+id/ET"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>
jinapanjianting.java
package com.example.asus.jichuzujian;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class jinapanjianting extends AppCompatActivity {
    private TextView mTextView;
    private EditText mEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jinapanjianting);
        mTextView = (TextView)findViewById(R.id.tv);
        mEditText = (EditText)findViewById(R.id.ET);
        mEditText.addTextChangedListener(mTextWatcher);
    }
    TextWatcher mTextWatcher = new TextWatcher() {
        private CharSequence temp;
        private int editStart ;
        private int editEnd ;
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            temp = s;
            Toast.makeText(getApplicationContext(),"1",Toast.LENGTH_LONG).show();

        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub
//   mTextView.setText(s);//将输入的内容实时显示
        }
        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            editStart = mEditText.getSelectionStart();
            editEnd = mEditText.getSelectionEnd();
            mTextView.setText("您输入了" + temp.toString()+ "个字符");
            Toast.makeText(getApplicationContext(),"1",Toast.LENGTH_LONG).show();
//            if (temp.length() > 10) {
//                Toast.makeText(jinapanjianting.this,
//                        "你输入的字数已经超过了限制!", Toast.LENGTH_SHORT)
//                        .show();
//                s.delete(editStart-1, editEnd);
//                int tempSelection = editStart;
//                mEditText.setText(s);
//                mEditText.setSelection(tempSelection);
//            }
        }
    };
}

上边temp.toString()是用来获取内容的,
temp.length()可以实时获取输入内容的长度。

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <!-- 语言选择器 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:orientation="horizontal"> <Spinner android:id="@+id/language_spinner1" android:layout_width="0dp" android:layout_height="50dp" android:layout_marginRight="10dp" android:layout_weight="1" /> <Spinner android:id="@+id/language_spinner2" android:layout_width="0dp" android:layout_height="50dp" android:layout_marginLeft="10dp" android:layout_weight="1" /> </LinearLayout> <!-- 消息列表(占满中间空间) --> <ListView android:id="@+id/messageListView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:divider="@android:color/transparent" android:dividerHeight="8dp" android:padding="16dp" /> <!-- 用ScrollView包裹输入区域,确保键盘弹出时可滚动 --> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:fillViewport="true"> <!-- 关键:确保ScrollView占满可用空间 --> <!-- 输入区域 --> <LinearLayout android:id="@+id/line1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#F5F5F5" android:orientation="horizontal" android:padding="8dp"> <!-- 语音/键盘切换按钮 --> <ImageButton android:id="@+id/voiceTextToggleButton" android:layout_width="45dp" android:layout_height="45dp" android:layout_gravity="center_vertical" android:background="@mipmap/icon_shengbo" android:padding="8dp" /> <!-- 文字输入框 --> <EditText android:id="@+id/textInputEditText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:layout_weight="1" android:background="@android:color/white" android:ems="10" android:gravity="top" android:hint="输入消息..." android:minHeight="48dp" android:padding="12dp" /> <!-- 语音输入按钮(默认隐藏) --> <Button android:id="@+id/recordButton" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:layout_weight="1" android:text="按住 说话" android:textColor="#FFFFFF" android:visibility="gone" android:textSize="16sp" /> </LinearLayout> </ScrollView> </LinearLayout>帮我解决点击输入框后键盘遮挡输入
最新发布
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷咕咕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值