0

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

4

1 回答 1

1

您可以选择 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>
于 2013-04-05T12:41:00.197 回答