5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AndroidStudioプラグイン開発で追加した独自メニューの表示/非表示を切り替える

Last updated at Posted at 2015-10-30

strings.xmlにフォーカスした状態で右クリックした時のみメニューを表示したいような場合の対応方法です。

strings_xml_-KotohaAndroid-___work_private_kotoha-android.png

結論

わかってしまえば簡単なんですが、実装したActionクラスの update() のパラメタの AnActionEvent を使ってvisibleをセットするだけです。

event.getPresentation().setVisible(visible);

例えば strings.xml にフォーカスした状態で右クリックした時のみメニューを表示したいような場合はこんな感じです。

package com.konifar.alice;

import com.intellij.codeInsight.actions.SimpleCodeInsightAction;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiUtilBase;
import org.jetbrains.annotations.Nullable;

public class SampleAction extends AnAction {

    // 略

    @Override
    public void update(AnActionEvent event) {
        final VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(event.getDataContext());
        event.getPresentation().setVisible(isStringXml(file));
    }

    private static boolean isStringXml(@Nullable VirtualFile file) {
        return file != null && "strings.xml".equals(file.getName());
    }

}

これで実行すると、クラスの中で右クリックしてもコンテキストメニューに表示されなくなります。

MainActivity_java_-KotohaAndroid-___work_private_kotoha-android.png

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?