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();  
                  
                }  
                
            }
        
    }
  • 相关阅读:
    一起谈.NET技术,WPF 自定义快捷键命令(Command) 狼人:
    一起谈.NET技术,WPF 基础到企业应用系列5——WPF千年轮回2 狼人:
    一起谈.NET技术,asp.net页面中输出变量、Eval数据绑定等总结 狼人:
    单片机沉思录——再谈static
    Java平台对脚本语言支持之ScriptEngine创建方式
    [置顶] [Html] Jquery那些事
    codeforces 165E Compatible Numbers
    2013第六周上机任务【项目2 程序填空(1)】
    腾讯再否认微信收费 三大运营商态度分化
    电子钟程序
  • 原文地址:https://www.cnblogs.com/84126858jmz/p/5329309.html
Copyright © 2011-2022 走看看