您的位置:首页 > 移动开发 > Android开发

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

2014-04-21 00:41 525 查看
移除EditText上的输入焦点的方法有很多种,下面介绍一种简单实用的方法。

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

EditTextDemoActivity.java

package com.android.EditTextDemo.activity;

import android.app.Activity;
import android.os.Bundle;

public class EditTextDemoActivity extends Activity {
/** Called when the activity is first created. */
@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>

效果图:





本文出自 “IT的点点滴滴” 博客,请务必保留此出处/article/4132184.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: