安卓的EditText监听光标移动 [英] Android EditText listener for cursor position change

查看:2180
本文介绍了安卓的EditText监听光标移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EditText上它的对话框。已填充的EditText上被创建时。当用户将光标上或附近的文本吐司将弹出的某些部分。

我的问题是监听改变光标位置。另<一href="http://stackoverflow.com/questions/3652569/android-edittext-listener-for-cursor-position-change">post问同样的问题,接受的解决方案是

  

您可以覆盖onSelectionChanged(INT SelStart的,INT selEnd)得到通知有关选择更改。如果光标被移动,这被称为以及(在此情况下SelStart的== selEnd)

<一个href="http://developer.android.com/reference/android/widget/TextView.html#onSelectionChanged%28int,%20int%29">onSelectionChanged (INT SelStart的,INT selEnd)是TextView的类的受保护方法。如何改写呢?

解决方案,能为我工作... * 大师你好,谢谢你们的回复,它的工作。 这是我做的详细,如果其他人有兴趣... 的*

第一步:创建子类

 包com.example;

进口android.content.Context;
进口android.util.AttributeSet;
进口android.widget.EditText;
进口android.widget.Toast;

公共类EditTextCursorWatcher扩展的EditText {

    公共EditTextCursorWatcher(上下文的背景下,ATTRS的AttributeSet,
            INT defStyle){
        超(背景下,ATTRS,defStyle);

    }

    公共EditTextCursorWatcher(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);

    }

    公共EditTextCursorWatcher(上下文的背景下){
        超(上下文);

    }


     @覆盖
     保护无效onSelectionChanged(INT SelStart的,INT selEnd){
        Toast.makeText(的getContext(),SelStart的为+ SelStart的+selEnd为+ selEnd,Toast.LENGTH_LONG).show();
         }
}
 

第二步:引用类在布局文件(如main.xml中(虽然我的是一个自定义对话框布局))。不要忘了使用全包名称(在这种情况下com.example.EditTextCursorWatcher,如:

 &LT; com.example.EditTextCursorWatcher
     机器人:ID =@ + ID / etEdit
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:重力=顶
    安卓的minlines =5
    机器人:inputType =textMultiLine/&GT;
 

解决方案

刚子类或扩展类的EditText并添加以下code到新创建的类:

  @覆盖
 保护无效onSelectionChanged(INT SelStart的,INT selEnd){
        //这里做UR的任务。
    }
 

不要忘了构造函数添加到子类。 :)

I have a dialog with EditText in it. The EditText is already populated when it is created. When the user places the cursor on or near certain parts of the text a Toast will pop up.

My problem is listening for changes in cursor position. Another post asks the same question and the accepted solution was

You can override onSelectionChanged (int selStart, int selEnd) to get notified about selection changes. If the cursor is moved, this is called as well (in this case selStart == selEnd)

onSelectionChanged (int selStart, int selEnd) is a protected method of the TextView class. How do override it?

Solution that worked for me ... *Hi Guru, thankyou for your reply, it worked. Here is what I did in detail if anyone else is interested...*

Step One: Create the sub class

package com.example;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.Toast;

public class EditTextCursorWatcher extends EditText {

    public EditTextCursorWatcher(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);

    }

    public EditTextCursorWatcher(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public EditTextCursorWatcher(Context context) {
        super(context);

    }


     @Override   
     protected void onSelectionChanged(int selStart, int selEnd) { 
        Toast.makeText(getContext(), "selStart is " + selStart + "selEnd is " + selEnd, Toast.LENGTH_LONG).show();
         } 
}

Step Two: refer to the class in the layout file (eg main.xml (though mine was a custom dialog layout)). Don't forget to use full package name (in this case com.example.EditTextCursorWatcher, eg

    <com.example.EditTextCursorWatcher
     android:id="@+id/etEdit"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="top"
    android:minLines="5"
    android:inputType="textMultiLine"/> 

解决方案

Just subclass or extend the class EditText and add the following code to the newly create class:

 @Override 
 protected void onSelectionChanged(int selStart, int selEnd) {
        // Do ur task here.
    }

Don't forget to add constructors to the subclass. :)

这篇关于安卓的EditText监听光标移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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