我有TextView
。当长按它时,应突出显示完成长按的文本,并且应该出现一个上下文操作菜单,它提供了一些额外的选项。
但是,如果文本突出显示,则上下文操作菜单与所需内容不同。另一方面,如果显示了所需的上下文操作菜单,则不突出显示该文本。
我已将其归零到return
中onLongClick
函数中的onLongClickListener
语句。具体来说,如果onLongClick
函数returns true
(消耗点击),则会显示上下文操作菜单,但由于长按不会传递给文本选择工具,因此不会激活它。但是,如果onLongClick
函数返回false
,则会将单击传递给一个函数,该函数会触发copy + paste
带有选择的文本的默认行为。
代码:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class HelloTxtView extends Activity {
//private EditText ed;
private TextView ed;
//actionmode callback.
private ActionMode mActionMode;
private ActionMode.Callback mActionModeCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_txt_view);
ed = (TextView) findViewById(R.id.txtview);
ed.setFocusable(true);
ed.setText("Hello World! Let's select some text!!");
initActionModeCallbacks();
ed.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Log.v(this.toString(), "Long click.");
ed.setCursorVisible(true);
Log.v(this.toString(), "Starting actionmodecallback.");
mActionMode = HelloTxtView.this.startActionMode(mActionModeCallback);
v.setSelected(true);
return false;
}
});
}
public void initActionModeCallbacks() {
/*
* This function initializes the callbacks.
*/
mActionModeCallback = new ActionMode.Callback() {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
//nothing to do here.
Log.v(this.toString(), "Preparing action mode.");
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
Log.v(this.toString(), "Destroy action mode.");
//mActionModeCallback = null;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
Log.v(this.toString(), "Creating new action mode menu.");
//inflate a new menu.
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
Log.v(this.toString(), "Done inflating menu.");
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Log.v(this.toString(), "An item was clicked.");
switch(item.getItemId()) {
case R.id.dictLookup:
Log.v(this.toString(), "Look up dictionary.");
break;
case R.id.readFromHere:
Log.v(this.toString(), "Start reading from here:" + ed.getSelectionStart());
}
return false;
}
};
}
}
我的问题:
1.如何覆盖TextView
的默认行为?
2.如何通过上下文操作菜单选择文本?
答案 0 :(得分:1)
如果你想在文本选择上获得自定义上下文动作模式,那么你可以像这样做
ed = (TextView) findViewById(R.id.txtview);
ed.setText("Hello World! Let's select some text!!");
initActionModeCallbacks();
ed.setTextIsSelectable(true);
ed.setCustomSelectionActionModeCallback(mActionModeCallback);
ed.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Log.v(this.toString(), "Long click.");
ed.setCursorVisible(true);
v.setSelected(true);
return false;
}
});
并在onCreateActionMode(ActionMode模式,菜单菜单)中,如果要删除标准选择项,可以调用
menu.clear();
另请注意,菜单中的项目需要有SHOW_AS_ACTION_ALWAYS标志,因为溢出按钮不起作用导致焦点更改https://stackoverflow.com/a/9883763/2751697