gpt4 book ai didi

java - 具有多个 MenuItem 的 ActionBarSherlock?

转载 作者:太空宇宙 更新时间:2023-11-03 11:52:17 25 4
gpt4 key购买 nike

我一直在我的一个应用程序中使用带有两个 MenuItem 的 ABS 4.0,但发现了一个小错误:按下第二个 MenuItem 时,它与第一个完全相同......

我已经尝试了几乎所有我能想到的方法,但它不起作用。我已经更改了 onOptionItemSelected,因为我认为这是我需要编辑的方法。

编辑:

我一直在查看@Ollie 的建议,但 LogCat 和 Debug 都没有显示出奇怪的东西。也许它在代码的其他部分,或者 ABS 的声明中?这是完整的代码,如果你能看一遍,那就太好了!

整个 Activity 的代码,因为它可能在其他地方?

package bas.sie.Antonius;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

public class TeacherInfo extends SherlockActivity {

String URLhome;
String Info;
String TeacherAb;
TextView mTxtvInfo;
Button mBtnTeacherStSchedule;
Button mBtnTeacherDaySchedule;
private static String mainUrl = "http://www.carmelcollegegouda.nl/site_ant/";
private static String endUrl = ".htm";
private static String[] myUrls = { "roosters/dagroosters/Doc_V1_",
"roosters/standaardroosters/Doc1_" };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contactinfo);
setTitle("Over deze leraar");

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

mTxtvInfo = (TextView) findViewById(R.id.TxtvTeacher);

Intent startingIntent = getIntent();
Info = startingIntent.getStringExtra("contact");
mTxtvInfo.setText(Info);

Intent startingIntent1 = getIntent();
TeacherAb = startingIntent1.getStringExtra("abbrev");

mBtnTeacherDaySchedule = (Button) findViewById(R.id.btnTeacherDaySchedule);
mBtnTeacherStSchedule = (Button) findViewById(R.id.btnTeacherStSchedule);

mBtnTeacherDaySchedule.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

URLhome = makeUrl(0);

Intent i = new Intent(TeacherInfo.this, MyWebView.class);
i.putExtra("home", URLhome);
startActivityForResult(i, 0);
}
});

mBtnTeacherStSchedule.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

URLhome = makeUrl(1);

Intent i = new Intent(TeacherInfo.this, MyWebView.class);
i.putExtra("home", URLhome);
startActivityForResult(i, 0);
}
});

}

private String makeUrl(int index) {
String s = mainUrl + myUrls[index] + TeacherAb + endUrl;
return s;
}// makeurl

@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Instellingen")
.setIcon(R.drawable.ic_settings)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add("Over de app")
.setIcon(R.drawable.ic_about)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, AntoniusActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.settings:
Intent i = new Intent(this, About.class);
startActivity(i);
return true;
case R.id.about:
Intent about = new Intent(this, About.class);
startActivity(about);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

}

我认为问题出在菜单项的声明中,但我没有看到那里有任何问题...

你能看看我的 menu.xml 吗?发布在这里:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/settings"
android:icon="@drawable/ic_settings"
android:title="Instellingen"></item>
<item android:id="@+id/about"
android:icon="@drawable/ic_about"
android:title="Over de app"></item>
</menu>

最佳答案

像这样创建菜单:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}

然后使用 switch 语句来处理选择:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// Do stuff
return true;
case R.id.menu_item_2:
// Do stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}

编辑:最后,您应该为每个项目做不同的事情,如果您将 Intent 目标 Activity 更改为另一个,它会按照您的预期进行:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// ... Stuff ...
case R.id.settings: // Settings item
Intent i = new Intent(this, About.class); // Start About.java Activity, but item says "settings"
// TODO: Change About to Settings?
i = new Intent(this, Settings.class);
startActivity(i);
return true;

case R.id.about: // About item
Intent about = new Intent(this, About.class); // Start About.java Activty
startActivity(about);
return true;

default:
return super.onOptionsItemSelected(item);
}
}

关于java - 具有多个 MenuItem 的 ActionBarSherlock?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9774815/

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