gpt4 book ai didi

java - 如何从 NavDrawer 中的项目单击开始新 Activity

转载 作者:行者123 更新时间:2023-12-01 12:05:52 26 4
gpt4 key购买 nike

我是新手。我只是想知道如何通过单击抽屉导航内的选项之一来启动新的(预定义的) Activity ?我刚刚完成了有关如何制作 NavDrawer 的教程。这是到目前为止我的主要 Activity (称为 IntroActivity)

IntroActivity.java:

public class IntroActivity extends Activity {

private String[] drawerListViewItems;
private ListView drawerListView;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);

//Retrieve List items from strings.xml
drawerListViewItems = getResources().getStringArray(R.array.list_items);

//Retrieve ListView defined in intro_activity.xml
drawerListView = (ListView) findViewById(R.id.left_drawer);

//Set the adapter for the list view
drawerListView.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_listview_item, drawerListViewItems));

//App Icon
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

actionBarDrawerToggle = new ActionBarDrawerToggle(
this, /* Host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* Image to replace "up" image*/
R.string.drawer_open, /* Open drawer description*/
R.string.drawer_close /* Close drawer description*/
);

//Set actionBarDrawerToggle as the DrawerListener
drawerLayout.setDrawerListener(actionBarDrawerToggle);

getActionBar().setDisplayHomeAsUpEnabled(true);

//ADD SHADOW TO THE RIGHT EDGE OF DRAWER
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

//Responding to clicks implementation:
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
//Sync the toggle state after onRestart... has occurred
actionBarDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

actionBarDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.intro, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
//Call ActionBarDrawerToggle.onOptionsItemSelected(),
//If it returns true, then it has handles the app icon touch event
if (actionBarDrawerToggle.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) {
//I WOULD LIKE TO REPLACE THE FOLLOWING CODE WITH SOMETHING
//THAT CAN START A NEW ACTIVITY
/*Toast.makeText(IntroActivity.this, ((TextView)view).getText(),
Toast.LENGTH_LONG).show(); */

drawerLayout.closeDrawer(drawerListView);
}
}

}

如果您能提供帮助,谢谢。

最佳答案

使用这个

Intent intent = new Intent(IntroActivity.this, YourNewActivity.class);
startActivity(intent);

其中 YourNewActivity 是您要启动的 Activity

关于java - 如何从 NavDrawer 中的项目单击开始新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27606862/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com