gpt4 book ai didi

Android onOptionsItemSelected

转载 作者:行者123 更新时间:2023-11-29 21:30:44 27 4
gpt4 key购买 nike

我对这段代码感到困惑,我有 6 个菜单项,每个菜单项我希望它在 Web View 上加载不同的网页,但我看不到我在哪里可以说当选择第 3 项时执行此操作,有人可以帮忙吗?

@Override
public boolean onOptionsItemSelected(MenuItem item) {

// call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
// then it has handled the app icon touch event

if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}

请不要为此辱骂我,我只是需要帮助:/

我的 strings.xml

<string-array name="items">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
<item>Item 4</item>
<item>Item 5</item>
<item>Item 6</item>
</string-array>

所以每次我点击不同的项目时,它都会执行 mWebView.loadUrl("");

完整代码:

 package com.purelymean.purelymean;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {

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

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com/");
mWebView.setWebViewClient(new WebViewClient());

// get list items from strings.xml
drawerListViewItems = getResources().getStringArray(R.array.items);
// get ListView defined in activity_main.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));

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

// 2.1 create ActionBarDrawerToggle
actionBarDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
);

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

// 2.3 enable and show "up" arrow


// just styling option
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

drawerListView.setOnItemClickListener(new DrawerItemClickListener());


}



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

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


@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.first:
// do something based on first item click
return true;
case R.id.second:
// do something based on second 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) {
Toast.makeText(Main.this, ((TextView)view).getText(), Toast.LENGTH_LONG).show();
drawerLayout.closeDrawer(drawerListView);

}
}


}

最佳答案

您应该实现 onItemClick()DrawerItemClickListener如下。 onOptionsItemSelected()方法应保持原样。

private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
if(position == 0){
// first item clicked, open url
} else if(position == 1){
// second item clicked, open url
}
drawerLayout.closeDrawer(drawerListView);

}
}

可以找到很详细的教程here .

你可以通过给每个项目一个单独的 id,然后在 onOptionsItemSelected() 中检查它来做到这一点像这样:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.first:
// do something based on first item click
return true;
case R.id.second:
// do something based on second item
return true;
}

return super.onOptionsItemSelected(item);
}

<罢工>

关于Android onOptionsItemSelected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19619500/

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