在我的Android应用程序中,我有一个菜单项logout。我想添加另一个菜单项My Panel,它重定向到管理面板页面gmumbai.co.in/admin。如何实现这一点。请帮我一个示例代码谢谢你。
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_overview, 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.
int id = item.getItemId();
Intent intent;
switch (item.getItemId()) {
case R.id.action_logout:
app = ((MyApplication) getApplicationContext());
app.logOut();
intent = new Intent(overview.this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
overview.this.startActivity(intent);
overview.this.finish();
return true;
case R.id.action_MyPanel:
Uri uri=Uri.parse("http://gmumbai.co.in/admin");
overview.this.startActivity(new Intent (Intent.ACTION_VIEW,uri));
overview.this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context="com.gmumbai.gvendor.overview">
<item android:id="@+id/action_logout" android:title="@string/action_logout"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/action_MyPanel" android:title="@string/action_MyPanel"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
答案 0 :(得分:0)
要添加菜单条目,请尝试:
<强> RES /菜单/ main_menu.xml 强>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item
android:id="@+id/action_logout"
android:icon="@drawable/ic_logout"
android:title="Logout"
app:showAsAction="always|collapseActionView" />
<item
android:id="@+id/action_redirAdmin"
android:icon="@drawable/admin_console"
android:title="Admin Console"
app:showAsAction="always|collapseActionView">
</item>
</menu>
<强> MainActivity.java 强>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_logout:
//Logout
return true;
case R.id.action_redirAdmin:
//Goto AdminConsole
//startActivity(new Intent(MainActivity.this, AdminConsole.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
如果您尝试在视图中显示网页,请确保在AdminConsole.java中有一个WebView
要添加WebView,请尝试以下操作:
<强> admin_console_activity.xml 强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<WebView
android:id="@+id/adminConsoleWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" />
</RelativeLayout>
<强> AdminConsole.java 强>
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admin_console_activity);
mWebView = (WebView) findViewById(R.id.adminConsoleWebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://gmumbai.co.in/admin");
}