我在我的項目中有一個抽屜佈局。如何將項目的事件放入抽屜佈局中?
我想通過點擊drawerlayout中的項目來放置一些事件。
我想我應該使用onNavigationItemSelected(MenuItem menuItem)
方法,但我不知道該怎麼做。
例如,我想在點擊其中一個項目並顯示一些文本視圖並放置按鈕引導用戶訪問我的網站之後顯示一個對話框。
或者我想通過推送其中一個項目來引導他們進行其他活動。
我在我的項目中有一個抽屜佈局。如何將項目的事件放入抽屜佈局中?
我想通過點擊drawerlayout中的項目來放置一些事件。
我想我應該使用onNavigationItemSelected(MenuItem menuItem)
方法,但我不知道該怎麼做。
例如,我想在點擊其中一個項目並顯示一些文本視圖並放置按鈕引導用戶訪問我的網站之後顯示一個對話框。
或者我想通過推送其中一個項目來引導他們進行其他活動。
爲了你而寫的代碼就不會感到困惑,我建議你使用默認導航抽屜在你的應用程序
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.your_id){
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setMessage("Write your message here.");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder1.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
//or if your want to call new activity, do this just call the intent
Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
,你可以找到在res/menu/activity_main_drawer.xml
編輯: 這是如何帶領用戶到您的網站:
yourButton.setOnClickListener(new OnClickListener){
@Override
public void onClick(View v) {
String url = "your_website_url";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent); // on fragment do : getActivity.startActivity(intent);
}
}
希望工程:)
試試這個
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_item_id1) {
// Handle the action
Toast.makeText(getApplicationContext() ,"nav_item_id1 Clicked" ,Toast.LENGTH_LONG).show();
} else if (id == R.id.nav_item_id2) {
// Handle the action
Toast.makeText(getApplicationContext() ,"nav_item_id2 clicked" ,Toast.LENGTH_LONG).show();
}
}
}
希望這有助於是你
顯示你需要什麼 – Jas