0

Androidでは、テキストビューが選択されている場合、単語が選択され、コンテキストアクションバーが上部に表示されます...その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 に答える