下面这段代码的意思 public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_add) { Intent intent = new Intent(this, AddActivity.class); startActivity(intent); //startActivity方法 return true; } else if (id == R.id.action_back) { Intent intent = new Intent(this, FragActivity.class); startActivity(intent); //startActivity方法 return true; } return super.onOptionsItemSelected(item); }
时间: 2024-02-10 15:33:41 浏览: 80
这段代码是在 Android 应用程序中处理菜单项的点击事件。在 onOptionsItemSelected() 方法中,首先通过 getItemId() 方法获得点击的菜单项的 ID,然后根据 ID 进行不同的操作。在这个例子中,如果点击了菜单中的 "action_add" 项,则会创建一个 Intent 对象,该对象用于启动 AddActivity 类,最后通过 startActivity() 方法启动该 Activity。同样,如果点击了菜单中的 "action_back" 项,则会创建一个 Intent 对象,该对象用于启动 FragActivity 类。最后,如果没有匹配的菜单项,调用 super.onOptionsItemSelected(item) 方法以让父类处理菜单项的点击事件。
相关问题
安卓drowlayout的使用案例代码
以下是一个简单的安卓drowlayout的使用案例代码:
XML布局文件:
```
<?xml version="1." encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 主界面布局 -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- 侧滑菜单布局 -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff" />
</android.support.v4.widget.DrawerLayout>
```
Java代码:
```
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// 设置侧滑菜单的列表项
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
// 设置侧滑菜单的点击事件
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// 设置ActionBarDrawerToggle,实现侧滑菜单的打开和关闭
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
// 设置ActionBarDrawerToggle为DrawerLayout的监听器
mDrawerLayout.addDrawerListener(mDrawerToggle);
// 设置ActionBar的Home按钮可点击,并且显示侧滑菜单图标
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 处理侧滑菜单的点击事件
}
}
}
```
阅读全文