小编Way*_*ail的帖子

Android 4.0 EditText没有软键盘和光标定位

我试图定义一个EditText框,而不会在触摸框时自动显示软键盘.我还需要根据触摸显示和移动闪烁的光标.只需使用mText.setInputType(InputType.TYPE_NULL),在Android 4.0之前就可以轻松完成.这是抑制自动软键盘显示的唯一方法,但在Android 4.0中它还可以抑制闪烁的光标.但是,光标确实正确定位,mText.getSelectionStart()确实返回最后一个触摸位置.例如,如果我在包含"123"的EditText框中触摸"2"和"3"之间,则mText.getSelectionStart()正确返回2,即使没有显示任何光标.有没有办法以编程方式在该位置显示光标?

这是我用来测试EditText游标位置的代码:

public class TestCodeActivity extends Activity implements OnClickListener {
        private EditText mText;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                getApplicationContext();
                setContentView(R.layout.test);
                findViewById(R.id.Button01).setOnClickListener(this);
                findViewById(R.id.Button02).setOnClickListener(this);
                findViewById(R.id.Button03).setOnClickListener(this);
                findViewById(R.id.Button04).setOnClickListener(this);
                mText = (EditText) findViewById(R.id.editText1);
                mText.setInputType(InputType.TYPE_NULL);
        }

        @Override
        public void onClick(View v) {
            if (v.getTag().equals("Clear")) {
                mText.setText("");
            } else {
                String buttontag = v.getTag().toString();
                String str = mText.getText().toString();
                int cursor = mText.getSelectionStart();
                if(cursor==str.length())
                    str = str + buttontag;
                else
                    str = str.substring(0, cursor) + buttontag + str.substring(cursor, str.length());
                mText.setText(str);
                mText.setSelection(cursor+1);
                // ---> need …
Run Code Online (Sandbox Code Playgroud)

android cursor android-edittext

7
推荐指数
1
解决办法
3525
查看次数

Android OnTouchListener安排一个线程来定位光标

答案发布在" 在Android EditText中如何在设置后在OnTouchListener中获取光标位置 "表示将来可以为100MS安排一个线程,以便给Android时间更新EditText光标位置.没有提供关于如何实现这一目标的代码.我使用Toast尝试了以下测试代码来显示光标位置.更新触摸后不显示光标位置.有人可以更正此代码,以便在mText.setSelection(光标)中提供触摸位置吗?

OnTouchListener otl = new OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent evt) {
                    Runnable r = new Runnable()
                        {
                            public void run() 
                            {
                                int cursor = mText.getSelectionStart();
                                Toast.makeText(getApplicationContext(), "Cursor=" + cursor, Toast.LENGTH_SHORT).show();
                                mText.setSelection(cursor);
                            }
                        };
                        Handler handler = new Handler();
                        handler.postDelayed(r, 1500);
                        return true;
                    }
                 };
                mText.setOnTouchListener(otl);
Run Code Online (Sandbox Code Playgroud)

android cursor-position ontouchlistener android-edittext

0
推荐指数
1
解决办法
1483
查看次数