ContextMenu的xml實(shí)現(xiàn)

contextMenu是上下文菜單,我們希望實(shí)現(xiàn)這樣一種效果,點(diǎn)擊主界面上的一個(gè)按鈕,彈出一個(gè)上下文菜單,響應(yīng)上下文菜單中對(duì)應(yīng)條目的點(diǎn)擊事件。第一種方式主要利用xml文件實(shí)現(xiàn)
實(shí)現(xiàn)代碼

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.button);
        //設(shè)置上下文監(jiān)聽
        button.setOnCreateContextMenuListener(this);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                view.showContextMenu();
               // view.showContextMenu(x,y);
            }
        });
    }

    @Override//創(chuàng)建上下文菜單
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context,menu);
    }

    @Override//上下文菜單選項(xiàng)事件
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.text1:
                Toast.makeText(MainActivity.this,"我是1!",Toast.LENGTH_SHORT).show();
                break;
            case R.id.text2:
                Toast.makeText(MainActivity.this,"我是2!",Toast.LENGTH_SHORT).show();
                break;
            case R.id.text3:
                Toast.makeText(MainActivity.this,"我是3!",Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);
    }
}

實(shí)現(xiàn)過程1、找到Button控件button2、設(shè)置上下文菜單監(jiān)聽 button.setOnCreateContextMenuListener(this),也可以button.setOnCreateContextMenuListener(new onCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context,contextMenu);
}
});,不用this代替,直接實(shí)現(xiàn)onCreateContextMenu。
3、響應(yīng)button的點(diǎn)擊事件,在該事件中實(shí)現(xiàn)contextMenu的顯示。
button.showContextMenu(); 4、當(dāng)設(shè)置上下文監(jiān)聽時(shí),使用的是this時(shí),我們需要在實(shí)現(xiàn)兩個(gè)函數(shù)一個(gè)是OnCreateContextMenu,另一個(gè)是OnContextItemSelected(),函數(shù)一是創(chuàng)建上下文菜單,函數(shù)二是上下文菜單選項(xiàng)事件。創(chuàng)建上下文菜單時(shí),MenuInflate inflate = getMenuInflate();inflate.inflate(R.menu.contextMenu);的作用是給ContextMenu設(shè)置布局文件,當(dāng)觸發(fā)時(shí)顯示在界面上。
R.menu.context是我們自己寫的一個(gè)布局文件,其寫法是先在res中新建一個(gè)文件夾menu,在menu中新建一個(gè)布局文件,在這里我們起名叫context。context.xml文件中的內(nèi)容如下:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/text1"
        android:title="測(cè)試一" />
    <item
        android:id="@+id/text2"
        android:title="測(cè)試二"/>
    <item
        android:id="@+id/text3"
        android:title="測(cè)試三"/>
</menu>

item中的title就是上下文菜單所顯示的內(nèi)容

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容