ContextMenu长按事件
生活随笔
收集整理的這篇文章主要介紹了
ContextMenu长按事件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*
ContextMenu菜單就是長按某一個組件,就會在屏幕的中間彈出ContextMenu,這里設置為長按文本框彈出ContextMenu菜單*/public class MyContextMenu extends AppCompatActivity {/** Called when the activity is first created. */final static int CONTEXT_MENU_1 = Menu.FIRST;//可以理解為ID設置的最小數值final static int CONTEXT_MENU_2 = Menu.FIRST + 1;final static int CONTEXT_MENU_3 = Menu.FIRST + 2;TextView textView = null;//設置成員變量@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_my_context_menu);textView = (TextView)findViewById(R.id.label);//獲取textregisterForContextMenu(textView);//注冊長摁菜單事件}/*創建菜單*/@Overridepublic void onCreateContextMenu(ContextMenu menu,View v, ContextMenu.ContextMenuInfo menuInfo){menu.setHeaderTitle("快捷菜單標題");//設置標題//設置對應id及titlemenu.add(0, CONTEXT_MENU_1, 0,"菜單子項1");menu.add(0, CONTEXT_MENU_2, 1,"菜單子項2");menu.add(0, CONTEXT_MENU_3, 2,"菜單子項3");}/*設置對應事件*/@Overridepublic boolean onContextItemSelected(MenuItem item){switch(item.getItemId()){case CONTEXT_MENU_1:Toast.makeText(MyContextMenu.this,"選中菜單子項1",Toast.LENGTH_SHORT).show();return true;case CONTEXT_MENU_2:Toast.makeText(MyContextMenu.this,"選中菜單子項2",Toast.LENGTH_SHORT).show();return true;case CONTEXT_MENU_3:Toast.makeText(MyContextMenu.this,"選中菜單子項3",Toast.LENGTH_SHORT).show();return true;}return false;}public void skip(View v){Intent intent = new Intent();intent.setClass(MyContextMenu.this,TabDemo.class);//從前者跳往后者startActivity(intent);//啟動}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"tools:context=".MyContextMenu"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:id="@+id/label"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/></LinearLayout>
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的ContextMenu长按事件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 字符串分割--java中String.s
- 下一篇: 火狐最实用的几款插件介绍[含附件]