Android, how do I stop a pointer appearing below a

2019-01-12 10:56发布

问题:

I have an activity with some EditTexts in it. When I click on an EditText to change the text in it a blue arrow appears below where the cursor appears. How can I stop this appearing?

回答1:

As MarvinLabs pointed out, it's a set of drawables in the Android SDK. They can be overridden though.

  1. Find these images in your sdk platform:

    • text_select_handle_left.png
    • text_select_handle_middle.png
    • text_select_handle_right.png
  2. Copy them over to your drawables folder, so you have a local copy to reference. You can change the colors of these, make them empty, or whatever you want. Note that this as MarvinLabs said, it might not be wise to remove them completely because they help the user select text for cutting and copying.

  3. If you don't have a custom theme yet defined in your styles.xml, define one. Then add these items to it:

_

<style name="CustomTheme" parent="@android:style/Theme.Holo.Light">
  <item name="android:textSelectHandleLeft">@drawable/text_select_handle_left</item>
  <item name="android:textSelectHandleRight">@drawable/text_select_handle_right</item>
  <item name="android:textSelectHandle">@drawable/text_select_handle_middle</item>
</style>

BEFORE:

AFTER:

(different EditTexts but you get the idea)



回答2:

This pointer is a system helper to allow the user to move the cursor easily (more precisely than just by touching the EditText). So I don't know if you can remove it, but I am sure it is not a good idea anyway.

Additionally, this is the kind of pointer that will get styled differently according to the devices. If you look at the SDK's android source, you will find some drawables called text_select_handle_XXX.png I guess you will find from that how to change the system style in your own theme.



回答3:

I'm pretty sure this is only happening on that specific API or device. It might work differently on other apps too. Ultimately I don't see any real way to disable something that is native to the device. Also, the devices out there is very vast so you'd have a very hard time disabling that even if you find a way.



回答4:

You can do this programmatically with a custom EditText. You need to create a custom MovementMethod implementation, one such that canSelectArbitrarily() returns false. Then you simply override EditText.getDefaultMovementMethod() to return your CustomMovementMethod:

public class MyEditText extends EditText {
    private CustomMovementMethod customMovementMethod = new CustomMovementMethod();
    @Override 
    public MovementMethod getDefaultMovementMethod() {
        return customMovementMethod;
    }
}

private class CustomMovementMethod implements MovementMethod {

    @Override
    public boolean canSelectArbitrarily() {
        return false;
    }

    @Override
    public void initialize(TextView widget, Spannable text) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onGenericMotionEvent(TextView widget, Spannable text, MotionEvent event) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onKeyDown(TextView widget, Spannable text, int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onKeyOther(TextView view, Spannable text, KeyEvent event) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onKeyUp(TextView widget, Spannable text, int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onTakeFocus(TextView widget, Spannable text, int direction) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onTouchEvent(TextView widget, Spannable text, MotionEvent event) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onTrackballEvent(TextView widget, Spannable text, MotionEvent event) {
        // TODO Auto-generated method stub
        return false;
    }

}