禁用上下文菜单后,无法在EditText中选择文本

时间:2018-05-01 23:31:34

标签: android textselection

看到Disable EditText context menu之后。我能够隐藏上下文菜单但不幸的是我无法从edittext中选择文本。我该怎么做我的意思是选择文字?这是我的代码

import android.app.Activity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText=findViewById(R.id.myid);
    editText.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            return true;
        }
    });
    editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {

            menu.clear();
            MenuInflater inflater=mode.getMenuInflater();
            inflater.inflate(R.menu.mymenu,menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }
    });

}
}

这是xml文件

   <?xml version="1.0" encoding="utf-8"?>
   <android.support.constraint.ConstraintLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="pallob.example.com.customedit.MainActivity">

   <EditText
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:id="@+id/myid"
   android:gravity="top|left"
   />
 </android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

在onLongClick中将返回true更改为False。它会合作

答案 1 :(得分:0)

我找到了这个问题的解决方案,既隐藏了上下文菜单又选择了文本。我已经回答了Here