移除EditText上的输入焦点的方法有很多种,下面介绍一种简单实用的方法。
1.先看下面代码的在模拟器上运行的效果
EditTextDemoActivity.java
- package com.android.EditTextDemo.activity;
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
-
public class EditTextDemoActivity extends Activity {
-
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- }
main.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:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="@string/hello"
-
/>
-
<EditText
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
/>
-
<EditText
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
/>
-
</LinearLayout>
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
-
<resources>
-
<string name="hello">Hello EditText!</string>
-
<string name="app_name">EditTextDemo</string>
-
</resources>
效果图:
这时的光标是在第一个EditText闪烁的,第二个EditText却没有
2.将上面的main.xml改成如下所示,即是将第一个EditText的高度,宽度改为0dp,这样就能覆盖有光标闪烁的第一个EditText,从而达到了移除EditText上的输入焦点的效果
- <?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:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="@string/hello"
-
/>
-
<EditText
-
android:layout_width="0dp"
-
android:layout_height="0dp"
-
/>
-
<EditText
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
/>
-
</LinearLayout>
效果图:
本文转自 lingdududu 51CTO博客,原文链接:
http://blog.51cto.com/liangruijun/627850