请帮我解决这个问题: 我的项目中有textView,我想这样做:
我找到了选择部分的代码:
text= (TextView) findViewById(R.id.speech);
registerForContextMenu(text);
text.setTextIsSelectable(true);
显示默认Sellection texs
复制和选择所有操作。
现在我的问题是我如何自定义它并为其添加其他操作?
这是我的班级:
public class SellectedTexts extends AppCompatActivity implements ActionMode.Callback {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Log.d(Log, "onCreateActionMode");
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.text_select, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Log.d(Log, String.format("onActionItemClicked item=%s/%d", item.toString(), item.getItemId()));
switch(item.getItemId()) {
case R.id.save:
File outputTXT =new File(Environment.getExternalStorageDirectory(), "App Folder");
outputTXT.mkdir();
if (!outputTXT.exists() && !outputTXT.mkdirs()) { //Can't make path to save pic.
//Log.d(DEBUG_TAG, "Can't create directory to save image");
Toast.makeText(this, "File can't save.",Toast.LENGTH_LONG).show();
}
Calendar calender =Calendar.getInstance();
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyymmdd_hhmmss");
String fileName =outputTXT.getPath() + File.separator + simpleDateFormat.format(calender.getTime())+" .txt";
File pictureFile =new File(fileName);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(MainActivity.speech_text.getText().toString().getBytes());
fos.close();
Toast.makeText(this, "File save in ExternalCard/Type-Yar folder.",Toast.LENGTH_LONG).show();
} catch (Exception error) { //Can't save image.
// Log.d(DEBUG_TAG, "File not saved: " + error.getMessage());
Toast.makeText(this, "File can't save.", Toast.LENGTH_LONG).show();
}
mode.finish();// Finish and close the ActionMode
return true;
}
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
}
logcats:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: , PID: 5224
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:91)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
at android.widget.Toast.<init>(Toast.java:132)
at android.widget.Toast.makeText(Toast.java:434)
at .SellectedTexts.onActionItemClicked(SellectedTexts.java:57)
at android.widget.Editor$SelectionActionModeCallback.onActionItemClicked(Editor.java:3373)
at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onActionItemClicked(PhoneWindow.java:3946)
at android.support.v7.view.SupportActionModeWrapper$CallbackWrapper.onActionItemClicked(SupportActionModeWrapper.java:168)
at android.support.v7.app.AppCompatDelegateImplV7$ActionModeCallbackWrapperV7.onActionItemClicked(AppCompatDelegateImplV7.java:1703)
at android.support.v7.app.AppCompatDelegateImplV7$ActionModeCallbackWrapperV7.onActionItemClicked(AppCompatDelegateImplV7.java:1703)
at android.support.v7.view.StandaloneActionMode.onMenuItemSelected(StandaloneActionMode.java:136)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
at android.view.View.performClick(View.java:5254)
at android.view.View$PerformClick.run(View.java:21179)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6837)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
答案 0 :(得分:0)
希望这有帮助
检查这个答案https://stackoverflow.com/a/13004720/2826147,它对例子以及读出评论都有很好的解释。
TextView有一个方法setCustomSelectionActionModeCallback()
,应该使用startActionMode()
而不是bodyView.setCustomSelectionActionModeCallback(new StyleCallback());
。使用此功能可以自定义TextView用于文本选择的菜单。
示例代码:
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
Log.e("", String.format("onActionItemClicked item=%s/%d", item.toString(), item.getItemId()));
CharacterStyle cs;
TextView bodyView = null;
int start = bodyView.getSelectionStart();
int end = bodyView.getSelectionEnd();
SpannableStringBuilder ssb = new SpannableStringBuilder(bodyView.getText());
/*Toast.makeText(getActivity(), (String)data.result,
Toast.LENGTH_LONG).show();*/
Toast.makeText(this, " toast shown ", Toast.LENGTH_LONG).show();
switch (item.getItemId()) {
case R.id.bold:
cs = new StyleSpan(Typeface.BOLD);
ssb.setSpan(cs, start, end, 1);
bodyView.setText(ssb);
return true;
case R.id.italic:
cs = new StyleSpan(Typeface.ITALIC);
ssb.setSpan(cs, start, end, 1);
bodyView.setText(ssb);
return true;
case R.id.underline:
cs = new UnderlineSpan();
ssb.setSpan(cs, start, end, 1);
bodyView.setText(ssb);
return true;
}
return false;
}
如果您没有空间,则可以删除任何预先填充的菜单项,然后添加自己的。
// task to run the groovy script
task('Ls', type: JavaExec) {
environment 'gateway', props.gateway
environment 'username', props.username
environment 'password', props.password
main = 'Ls'
classpath = sourceSets.main.runtimeClasspath
}
task('Mkdir', type: JavaExec) {
environment 'gateway', props.gateway
environment 'username', props.username
environment 'password', props.password
main = 'Mkdir'
classpath = sourceSets.main.runtimeClasspath
}
task('Put', type: JavaExec) {
environment 'gateway', props.gateway
environment 'username', props.username
environment 'password', props.password
main = 'Put'
classpath = sourceSets.main.runtimeClasspath
}
task('Example') {
dependsOn Ls, Mkdir, Put
}