zoukankan      html  css  js  c++  java
  • 关于Android的EditText焦点问题

    当我们在写Android代码的时候,肯定会用到EditText控件,这是毫无疑问的。

    而如果我们的EditText一多,再加上TextView的话,界面上会很不好看。所以往往会用到文本框中的默认提示信息,当我们的EditText丢失焦点时,是默认提示信息,获得焦点时,默认提示信息消失。这时我们需要对EditText进行一个监听。

    如下是代码的实现:

    布局文件中的代码:

    <EditText android:id="@+id/edit"
    		android:layout_width="fill_parent"
    		android:layout_height="wrap_content"
    		
    	    android:hint="默认提示信息"
    	/>

    Java文件中的代码:

    final String string = edit.getHint().toString();
            daysEdit.setOnFocusChangeListener(new OnFocusChangeListener(){
    
                public void onFocusChange(View arg0, boolean hasFocus) {
                    if(hasFocus){
                    	edit.setHint(null);
                    }else{
                    	edit.setHint(string);
                    }
                }
            });

    如果你只是用到以上代码,会有一个很不友好的地方,那就是在程序刚启动后,我们的第一个EditText会默认获得焦点。也就是说这个时候,第一个EditText是不会有提示信息的,而且还会弹出输入框。

    这个时候我们还需要做一件事,那就是在它们的父控件中做如下设置:

    android:focusable="true"
    android:focusableInTouchMode="true"




  • 相关阅读:
    Python内置函数(49)——isinstance
    Python内置函数(48)——__import__
    Python内置函数(47)——vars
    Python内置函数(46)——format
    Python内置函数(45)——ascii
    Python内置函数(44)——len
    Python内置函数(43)——type
    Python内置函数(42)——hash
    Python内置函数(41)——id
    Linux下redis常用命令
  • 原文地址:https://www.cnblogs.com/fengju/p/6336166.html
Copyright © 2011-2022 走看看