当前位置:首页 > Android > 正文内容

EditText imeOptions属性

jsc9年前 (2016-04-14)Android4495

默认情况下软键盘右下角的按钮为“下一个”,点击会到下一个输入框,保持软键盘

1.png

设置 android:imeOptions="actionDone" ,软键盘下方变成“完成”,点击后光标保持在原来的输入框上,并且软键盘关闭

2.png

android:imeOptions="actionSend" 软键盘下方变成“发送”,点击后光标移动下一个

3.png

在 这里设置的imeOptions如何使用呢?如下面的代码,让EditText实现setOnEditorActionListener,在 onEditAction方法中actionId就对应我们设置的imeOptions。系统默认的actionId 有:EditorInfo.IME_NULL、EditorInfo.IME_ACTION_SEND、 EditorInfo.IME_ACTION_DONE等。这样我们就可以根据不同的EditText来实现不同的软键盘右下角功能键。

package com.test;

import com.test.main.TestAsyn;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class IMFActivity extends Activity implements OnEditorActionListener {
    
    EditText etDefault;
    EditText etEmail;
    EditText etNumber;
    
     /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imf_layout);
        
        etDefault = (EditText)findViewById(R.id.default_content);
        etEmail = (EditText)findViewById(R.id.email_content);
        etNumber = (EditText)findViewById(R.id.number_content);
        etDefault.setOnEditorActionListener(this);
        etEmail.setOnEditorActionListener(this);
        etNumber.setOnEditorActionListener(this);
        
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        switch(actionId){
        case EditorInfo.IME_NULL:
            System.out.println("null for default_content: " + v.getText() );
            break;
        case EditorInfo.IME_ACTION_SEND:
            System.out.println("action send for email_content: "  + v.getText());
            break;
        case EditorInfo.IME_ACTION_DONE:
            System.out.println("action done for number_content: "  + v.getText());
            break;
        }
        //Toast.makeText(this, v.getText()+"--" + actionId, Toast.LENGTH_LONG).show();
        return true;
    }
}

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <TableLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TableRow>
            <TextView android:text="No special rules" android:id="@+id/TextView01"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
            <EditText android:text="1111111111111" android:id="@+id/default_content"
                android:layout_width="fill_parent" android:layout_height="wrap_content"></EditText>
        </TableRow>
        <TableRow>
            <TextView android:text="Email address:" android:id="@+id/TextView01"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
            <EditText android:text="" android:id="@+id/email_content"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:inputType="text|textEmailAddress"
                android:imeOptions="actionSend"></EditText>
        </TableRow>
        <TableRow>
            <TextView android:text="Signed decimal number:" android:id="@+id/TextView01"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
            <EditText android:text="" android:id="@+id/number_content"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:inputType="number|numberSigned|numberDecimal"
                android:imeOptions="actionDone"></EditText>
        </TableRow>
    </TableLayout>
</ScrollView>


扫描二维码推送至手机访问。

版权声明:本文由微小站发布,如需转载请注明出处。

本文链接:https://jsc0.com/post/109.html

分享给朋友:

“EditText imeOptions属性” 的相关文章

ViewPager + HorizontalScrollView 实现可滚动的标签栏

ViewPager + HorizontalScrollView 实现可滚动的标签栏

这是一个可滑动的标签栏的自定义控件,参考此文章http://blog.csdn.net/fx_sky/article/details/8990573,我将主要的功能整合成一个类,配上2个特定的布局即可使用。 效果图:    主要布局文件:<?xml&nb...

Android采用SharedPreferences保存用户登录信息

Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。使用 SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data/<package name>/shared...

Android ImageView的scaleType属性与adjustViewBounds属性

Android ImageView的scaleType属性与adjustViewBounds属性

ImageView的scaleType的属性有好几种,分别是matrix(默认)、center、centerCrop、centerInside、fitCenter、fitEnd、fitStart、fitXY android:sca...

Android 更换皮肤思路及解决方案

Android 更换皮肤思路及解决方案

本篇博客要给大家分享的一个关于Android应用换肤的Demo,大家可以到我的github去下载demo,以后博文涉及到的代码均会上传到github中统一管理。 github地址:https://github.com/devilWwj/Android-skin-upda...

采用SharedPreferences保存用户偏好设置参数

采用SharedPreferences保存用户偏好设置参数-------------------------------------------------1.eclipse就是通过xml来保存用户的偏好设置-->window-->perfences-------------------...

Android SharedPreferences PreperenceScreen 偏好数据存取

SharedPreferences是一个接口,程序是无法创建SharedPreferences实例的,1、Context.getSharedPreferences(String name,int mode)来得到一个指定name的SharedPreferences实例2、PreferenceMana...