且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android开发学习笔记:如何移除EditText上的输入焦点

更新时间:2022-09-16 23:23:28

移除EditText上的输入焦点的方法有很多种,下面介绍一种简单实用的方法。

1.先看下面代码的在模拟器上运行的效果

EditTextDemoActivity.java


  1. package com.android.EditTextDemo.activity;  
  2.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.  
  6. public class EditTextDemoActivity extends Activity {  
  7.     /** Called when the activity is first created. */ 
  8.     @Override 
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.     }  

main.xml


  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello" 
  11.     /> 
  12. <EditText 
  13.     android:layout_width="fill_parent" 
  14.     android:layout_height="wrap_content" 
  15.     />   
  16. <EditText 
  17.     android:layout_width="fill_parent" 
  18.     android:layout_height="wrap_content" 
  19.     />     
  20. </LinearLayout> 

strings.xml


  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <string name="hello">Hello EditText!</string> 
  4.     <string name="app_name">EditTextDemo</string> 
  5. </resources> 

效果图:

这时的光标是在第一个EditText闪烁的,第二个EditText却没有

Android开发学习笔记:如何移除EditText上的输入焦点

 

 2.将上面的main.xml改成如下所示,即是将第一个EditText的高度,宽度改为0dp,这样就能覆盖有光标闪烁的第一个EditText,从而达到了移除EditText上的输入焦点的效果


  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello" 
  11.     /> 
  12. <EditText 
  13.     android:layout_width="0dp" 
  14.     android:layout_height="0dp" 
  15.     />   
  16. <EditText 
  17.     android:layout_width="fill_parent" 
  18.     android:layout_height="wrap_content" 
  19.     />     
  20. </LinearLayout> 

效果图:

Android开发学习笔记:如何移除EditText上的输入焦点



本文转自 lingdududu 51CTO博客,原文链接: 

http://blog.51cto.com/liangruijun/627850