zoukankan      html  css  js  c++  java
  • 把键盘的回车换成发送,监听键盘的弹起和隐藏

     <EditText
            android:id="@+id/mess"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:imeOptions="actionSend"
            android:inputType="text" />
    mess = (EditText) findViewById(R.id.mess);
            mess.setOnEditorActionListener(new OnEditorActionListener() {
                
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                       if (actionId == EditorInfo.IME_ACTION_SEND)     
                       {     
                         Toast.makeText(MessageActivity.this,"fasong",0).show();   
                       }     
                    return false;
                }
            });

    EditText通过设置android:imeOptions来改变默认的”文本或者样式。这里举几个常用的常量值:
    actionUnspecified  未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED. 
    actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE
    actionGo 去往,对应常量EditorInfo.IME_ACTION_GO
    actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH   
    actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND
    actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT 
    actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE

    监听键盘的弹起和隐藏

    package com.example.testjianpan;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnLayoutChangeListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.RelativeLayout;
    import android.widget.Toast;
    
    public class MainActivity extends Activity implements OnLayoutChangeListener{
        private EditText edt;
        private RelativeLayout rl;
         //屏幕高度  
        private int screenHeight = 0;  
        //软件盘弹起后所占高度阀值  
        private int keyHeight = 0;  
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            edt = (EditText) findViewById(R.id.edt);
            rl = (RelativeLayout) findViewById(R.id.rl);
              //获取屏幕高度  
            screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();  
            //阀值设置为屏幕高度的1/3  
            keyHeight = screenHeight/3; 
            Log.v("屏幕高度",screenHeight+"");
            rl.addOnLayoutChangeListener(this) ;
                    
        }
            @Override
            public void onLayoutChange(View v, int left, int top, int right,
                    int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                  //现在认为只要控件将Activity向上推的高度超过了1/3屏幕高,就认为软键盘弹起  
                if(oldBottom != 0 && bottom != 0 &&(oldBottom - bottom > keyHeight)){  
                      
                    Toast.makeText(MainActivity.this, "监听到软键盘弹起...", Toast.LENGTH_SHORT).show();  
                  
                }else if(oldBottom != 0 && bottom != 0 &&(bottom - oldBottom > keyHeight)){  
                      
                    Toast.makeText(MainActivity.this, "监听到软件盘关闭...", Toast.LENGTH_SHORT).show();  
                  
                }  
                
            }
        
    }
  • 相关阅读:
    MogoDb的使用及配置
    HttpClient中转上传文件
    springboot +Thymeleaf+UEditor整合记录
    Linux 下安装mysql
    Java开发微信公众号(五)---微信开发中如何获取access_token以及缓存access_token
    Java开发微信公众号(四)---微信服务器post消息体的接收及消息的处理
    Java开发微信公众号(三)---微信服务器请求消息,响应消息,事件消息以及工具处理类的封装
    Java开发微信公众号(二)---开启开发者模式,接入微信公众平台开发
    Java开发微信公众号(一)---初识微信公众号以及环境搭建
    mybatis sql转义符号
  • 原文地址:https://www.cnblogs.com/84126858jmz/p/5329309.html
Copyright © 2011-2022 走看看