Android的编辑文本 - 光标停留在起始位置 [英] Android Edit Text - Cursor stays at the starting position

查看:287
本文介绍了Android的编辑文本 - 光标停留在起始位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目使用编辑文本。

问题是,每当我键入任何内容到文本框中,它显示了,但光标不会从它的起始位置移动,无论我有多少个字符键入可言。另外,我无法通过单击文本框中任何特定的字符移动。

对于包含文字编辑我的XML文件是这样的

 <的EditText
        机器人:ID =@ + ID / XET
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =295dp
        机器人:layout_alignParentLeft =真
        机器人:EMS =10
        机器人:focusableInTouchMode =真
机器人:可聚焦=真正的>

XML文件是:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>< RelativeLayout的
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / xsubLayout
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:keepScreenOn =真
    机器人:方向=垂直>    < RelativeLayout的
        机器人:ID =@ + ID / relativeLayout1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentRight =真正的>    < / RelativeLayout的>    <的EditText
        机器人:ID =@ + ID / XET
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =295dp
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentRight =真
        机器人:layout_alignParentTop =真
        机器人:EMS =10
        安卓的inputType =textMultiLine>        < requestFocus的/>
    < /&的EditText GT;< / RelativeLayout的>    <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =WRAP_CONTENT机器人:ID =@ + ID / XK1
        机器人:layout_height =WRAP_CONTENT机器人:方向=垂直
        机器人:知名度=水涨船高>
        <包括机器人:ID =@ + ID / xKeyBoard布局=@布局/ GF>< / - 包括GT;
        <包括机器人:ID =@ + ID / xtop布局=@布局/顶>< / - 包括GT;
                <包括机器人:ID =@ + ID / X11布局=@布局/ X1>< / - 包括GT;
        <包括机器人:ID =@ + ID / X12布局=@布局/ X3>< / - 包括GT;
        <包括机器人:ID =@ + ID / X13布局=@布局/ X4>< / - 包括GT;
        <包括机器人:ID =@ + ID / X14布局=@布局/ X5>< / - 包括GT;        <包括机器人:ID =@ + ID / X15布局=@布局/ 5233>< / - 包括GT;
        <包括机器人:ID =@ + ID / X16布局=@布局/ X7>< / - 包括GT;
        <包括机器人:ID =@ + ID / X17布局=@布局/ X8>< / - 包括GT;
        <包括机器人:ID =@ + ID / X18布局=@布局/ X9>< / - 包括GT;        <包括机器人:ID =@ + ID / X19布局=@布局/ X10>< / - 包括GT;
        <包括机器人:ID =@ + ID / X20布局=@布局/ X11>< / - 包括GT;        <包括机器人:ID =@ + ID / X22布局=@布局/ X13>< / - 包括GT;        <包括机器人:ID =@ + ID / X23布局=@布局/ X14>< / - 包括GT;
        <包括机器人:ID =@ + ID / X24布局=@布局/ X15>< / - 包括GT;
        <包括机器人:ID =@ + ID / X25布局=@布局/ X16>< / - 包括GT;
        <包括机器人:ID =@ + ID / X26布局=@布局/ X17>< / - 包括GT;        <包括机器人:ID =@ + ID / X27布局=@布局/ X18>< / - 包括GT;
        <包括机器人:ID =@ + ID / X28布局=@布局/ X19>< / - 包括GT;
    < / RelativeLayout的>
< / RelativeLayout的>


解决方案

我用这办法解决这个问题,
如果字符是英文或数字字符然后更改的EditText重心向左别人在我的情况下,另一种语言是阿拉伯语,然后将改变重力左,光标将移动它根据每种语言的正确方法:

  editText.addTextChangedListener(新TextWatcher(){            @覆盖
            公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
                如果(s.length()大于0){
                    焦C = s.charAt(0);
                     //字符是Enlgish或数字。
                    如果((c基='A'和;和C< ='Z')||(c基='A'和;和C< ='Z')|| Character.isDigit( C)) {
                        editText.setGravity(Gravity.LEFT);
                    }其他{
                        editText.setGravity(Gravity.RIGHT);
                    }
                }
            }            @覆盖
            公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
                    INT后){            }            @覆盖
            公共无效afterTextChanged(编辑S){            }
        });

I am using an Edit Text in my project.

The problem is that whenever I type anything into the text box, it shows up, but the cursor does not move from its starting position at all, regardless of how many characters I type. Also I am not able to move by clicking to any particular character in the text box.

My xml file for containing the edit text is like this

<EditText
        android:id="@+id/xEt"
        android:layout_width="fill_parent"
        android:layout_height="295dp"
        android:layout_alignParentLeft="true"
        android:ems="10"
        android:focusableInTouchMode="true" 
android:focusable="true" >

The xml file is:

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

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/xsubLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:keepScreenOn="true"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true" >

    </RelativeLayout>

    <EditText
        android:id="@+id/xEt"
        android:layout_width="wrap_content"
        android:layout_height="295dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:inputType="textMultiLine" >

        <requestFocus />
    </EditText>

</RelativeLayout>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content" android:id="@+id/xK1"
        android:layout_height="wrap_content" android:orientation="vertical"
        android:visibility="gone">
        <include android:id="@+id/xKeyBoard" layout="@layout/gf"></include>
        <include android:id="@+id/xtop" layout="@layout/top"></include>
                <include android:id="@+id/x11" layout="@layout/x1"></include>
        <include android:id="@+id/x12" layout="@layout/x3"></include>
        <include android:id="@+id/x13" layout="@layout/x4"></include>
        <include android:id="@+id/x14" layout="@layout/x5"></include>

        <include android:id="@+id/x15" layout="@layout/x6"></include>
        <include android:id="@+id/x16" layout="@layout/x7"></include>
        <include android:id="@+id/x17" layout="@layout/x8"></include>
        <include android:id="@+id/x18" layout="@layout/x9"></include>

        <include android:id="@+id/x19" layout="@layout/x10"></include>
        <include android:id="@+id/x20" layout="@layout/x11"></include>

        <include android:id="@+id/x22" layout="@layout/x13"></include>

        <include android:id="@+id/x23" layout="@layout/x14"></include>
        <include android:id="@+id/x24" layout="@layout/x15"></include>
        <include android:id="@+id/x25" layout="@layout/x16"></include>
        <include android:id="@+id/x26" layout="@layout/x17"></include>

        <include android:id="@+id/x27" layout="@layout/x18"></include>
        <include android:id="@+id/x28" layout="@layout/x19"></include>


    </RelativeLayout>
</RelativeLayout>

解决方案

I am using this workaround for this problem, If characters are in English or digit characters then change EditText gravity to left else in another Language in my case it's in Arabic then will change gravity to left and the cursor will move in it's right way according to each language:

        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length() > 0){
                    char c = s.charAt(0);
                     // characters are Enlgish or digits.
                    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || Character.isDigit(c)) {
                        editText.setGravity(Gravity.LEFT);
                    }else {
                        editText.setGravity(Gravity.RIGHT);


                    }
                } 
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

这篇关于Android的编辑文本 - 光标停留在起始位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆