-
-
Save codeforfun-jp/a4214023411cf849d764bfa5fcfada04 to your computer and use it in GitHub Desktop.
【Java】How to create a context menu 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.contextmenusample; | |
import androidx.annotation.NonNull; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.ContextMenu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.Toast; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
registerForContextMenu(findViewById(R.id.rootLayout)); | |
} | |
@Override | |
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { | |
super.onCreateContextMenu(menu, v, menuInfo); | |
getMenuInflater().inflate(R.menu.context_menu, menu); | |
} | |
@Override | |
public boolean onContextItemSelected(@NonNull MenuItem item) { | |
// switch (item.getItemId()) { | |
// R.id.action_home -> | |
// R.id.action_user -> | |
// R.id.action_map -> | |
// } | |
String message = "「" + item.getTitle() + "」が押されました。"; | |
Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment