当前位置:网站首页>Use menu resources to implement option menus and context menus
Use menu resources to implement option menus and context menus
2022-06-28 23:58:00 【Standing on the shoulders of giants, review the old and know th】
1 Menu resource file
The menu resource file is in res/menu Under the table of contents , Root element usage <menu></menu> Mark , The tag can contain multiple <item></item> Mark , Used to define menu items .
<item> Common attributes of tags are shown in table 1:
attribute | describe |
android:id | Set up ID |
android:title | Set title |
android:alphabeticShortcut | Specify character shortcuts |
android:numericShortcut | Specify the numeric shortcut key |
android:icon | Assign icons |
android:enabled | Specifies whether menu items are available |
android:checkable | Specifies whether the menu item is optional |
android:checked | Specifies whether the menu item is selected |
android:visible | Specifies whether the menu item is visible |
surface 1 <item> Common attributes of tags
2 Use menu resources
2.1 Options menu
When the user clicks the menu button , The pop-up menu is the options menu .
OptionMenu: Implement the options menu
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = new MenuInflater(this);
menuInflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
Intent intent = new Intent(MainActivity.this, Settings.class);
startActivity(intent);
break;
case R.id.regard:
Intent intent1 = new Intent(MainActivity.this, Regard.class);
startActivity(intent1);
break;
}
return super.onOptionsItemSelected(item);
}
}https://github.com/hanyuhang-hz/android-demos
2.2 Context menu
When the user presses and holds the component , The pop-up menu is the context menu .
ContextMenu: Implement context menu
MainActivity.java
public class MainActivity extends Activity {
TextView introduce;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
introduce = (TextView) findViewById(R.id.introduce);
// Register context menu for text box
registerForContextMenu(introduce);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.introduce_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_copy:
Toast.makeText(MainActivity.this, " Copied ", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_collect:
Toast.makeText(MainActivity.this," Have collected ",Toast.LENGTH_SHORT).show();
break;
}
return true;
}
}边栏推荐
- stm32F407-------通用定时器
- Typescript -- Section 6 generic
- Scrapy使用xlwt实现将数据以Excel格式导出的Exporter
- ES6模块
- 小白创业做电商,选对商城系统很重要!
- Stm32f407 ------- RTC real time clock
- Baidu knows the crawler, and obtains the dialogue below the comment according to the question Id, clue ID and comment ID
- Is it safe to open a stock account on the Internet?
- MySQL connection query is easy to understand
- Implementation of dynamic timer for quartz
猜你喜欢

stm32F407-------RTC实时时钟

随笔记:插入排序 --from wcc

stm32F407-------通用定时器

从SQL注入绕过最新安全狗WAF中学习fuzz

三个pwn题

What are the virtual machine software? What are their respective roles?

6.28 learning content

Stm32f407 ------ clock system (systeminit clock initialization, systick tick timer)

Stm32f407----- register address name mapping analysis

Machine learning 6-decision tree
随机推荐
stm32F407-------IO引脚复用映射
The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (2)
Trois questions PWN
TypeScript -- 第六节 泛型
fio的IO重放功能
PHP function file_ get_ Contents and memory mapping of operating system
Is it safe to open an account for buying stocks online?
这玩意叫跳表?
Ahai's advice
Stm32f407----- register address name mapping analysis
Huawei's level 22 experts have worked hard for ten years to complete the advanced practical document of cloud native service grid. 6
Typescript-- section 4: Functions
MSYQL is abnormal. Don't worry. Mr. Allen will point out the puzzle
MNIST handwritten numeral recognition demo based on pytorch framework
自动化测试的生命周期是什么?
Analysis of CSRF Cross Site Request Forgery vulnerability
Along with the notes: methods simulating array like classes
【C Primer Plus第二章課後編程題】
【OpenCV】—线性滤波:方框滤波、均值滤波、高斯滤波
TypeScript--第五节:类