简体   繁体   English

Textview上的Android快速上下文栏

[英]Android Quick Contextual bar on textview

在Android中,如果选择了textview,则选择一个单词,并且上下文操作栏位于顶部...我想修改该CAB,使其看起来像快速操作栏...保持文本选择功能不变。 。请帮我...

you can select textview or anything like only in simple layout CAB appear in the top and there was some method onActionItemClicke that you want to click an item and then you modify your action in CAB default behavior................................ 您可以选择textview或仅在简单布局CAB之类的内容显示在顶部,并且有一些要单击项目的onActionItemClicke方法,然后您以CAB默认行为修改操作........... .....................

public class MainActivity extends Activity {
protected Object mActionMode;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    View view = findViewById(R.id.lay);
    view.setOnLongClickListener(new View.OnLongClickListener() {
        // Called when the user long-clicks on someView
        public boolean onLongClick(View view) {
          if (mActionMode != null) {
            return false;
          }

          mActionMode = MainActivity.this.startActionMode(mActionModeCallback);
              view.setSelected(true);
              return true;
            }
          });
        }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
  Toast.makeText(this, "Just a test", Toast.LENGTH_SHORT).show();
  return true;
}
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

    // Called when the action mode is created; startActionMode() was called
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
      // Inflate a menu resource providing context menu items
      MenuInflater inflater = mode.getMenuInflater();
      // Assumes that you have "contexual.xml" menu resources
      inflater.inflate(R.menu.cab, menu);
      return true;
    }

    // Called each time the action mode is shown. Always called after
    // onCreateActionMode, but
    // may be called multiple times if the mode is invalidated.
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
      return false; // Return false if nothing is done
    }

    // Called when the user selects a contextual menu item
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
      switch (item.getItemId()) {
      case R.id.toast:
        Toast.makeText(MainActivity.this, "Selected menu",
            Toast.LENGTH_LONG).show();
        mode.finish(); // Action picked, so close the CAB
        return true;
      case R.id.shan:
          Toast.makeText(MainActivity.this, "Selected shani",
                  Toast.LENGTH_LONG).show();
              mode.finish(); // Action picked, so close the CAB
              return true;
      default:
        return false;
      }
    }

    // Called when the user exits the action mode
    public void onDestroyActionMode(ActionMode mode) {
      mActionMode = null;
    }
  };

} }

and in menu xml file create menu that you would like to appear in top CAB



    <?xml version="1.0" encoding="utf-8"?>
        <menu xmlns:android="http://schemas.android.com/apk/res/android" >
  <item
    android:id="@+id/toast"
    android:title="Toast">
</item>
 <item
    android:id="@+id/shan"
    android:title="shani">
</item>
   </menu>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM