我是Android开发的新手,我试图更改弹出菜单项标题。我使用onCreateOptionsMenu(),但它不改变标题。怎么实现这个?代码包括......
MovieDetailsActivity.java
public class MovieDetailsActivity extends AppCompatActivity {
Button magnet;
Button torrent;
private String size720p,size1080p;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.movie_details_activity);
final Context context = this;
final Toolbar toolbar = (Toolbar)findViewById(R.id.MyToolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapse_toolbar);
Bundle bundle = getIntent().getExtras();
//Extract the data…
if (bundle != null) {
String title = bundle.getString("movie_title");
String screenshot = bundle.getString("screenshot");
String yts_web = bundle.getString("yts_web");
String rating = bundle.getString("rating");
String imdb_url = bundle.getString("imdb_url");
String mpa_rating = bundle.getString("mpa_rating");
String runtime = bundle.getString("runtime");
String genres = bundle.getString("genres");
String synopsis = bundle.getString("synopsis");
String poster = bundle.getString("poster");
final String youtube = bundle.getString("youtube");
size720p = (String) bundle.getString("size720p");
final String magnet720p = bundle.getString("magnet720p");
final String torrent720p = bundle.getString("torrent720p");
size1080p = bundle.getString("size1080p");
final String magnet1080p = bundle.getString("magnet1080p");
final String torrent1080p = bundle.getString("torrent1080p");
magnet = (Button) findViewById(R.id.magnet);
magnet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Creating the instance of PopupMenu
Context wrapper = new ContextThemeWrapper(context, R.style.popupMenuStyle);
PopupMenu popup = new PopupMenu(wrapper, magnet);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_dl_quality, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.hd) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse(magnet720p));
startActivity(intent);
} else if (item.getItemId() == R.id.full_hd) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse(magnet1080p));
startActivity(intent);
}
return true;
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method
torrent = (Button) findViewById(R.id.torrent);
torrent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Creating the instance of PopupMenu
Context wrapper = new ContextThemeWrapper(context, R.style.popupMenuStyle);
PopupMenu popup = new PopupMenu(wrapper, torrent);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_dl_quality, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.hd) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse(torrent720p));
startActivity(intent);
} else if (item.getItemId() == R.id.full_hd) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse(torrent1080p));
startActivity(intent);
}
return true;
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method
collapsingToolbarLayout.setTitle(title);
TextView Rating = (TextView)findViewById(R.id.imdb);
Rating.setText(rating);
TextView MpaaRating = (TextView)findViewById(R.id.mpaRating);
MpaaRating.setText(mpa_rating);
TextView Runtime = (TextView)findViewById(R.id.runtime);
Runtime.setText(runtime);
TextView Genres = (TextView)findViewById(R.id.genres);
Genres.setText(genres);
ImageView Poster = (ImageView)findViewById(R.id.moviePoster);
Glide.with(Poster.getContext()).load(poster).placeholder(R.drawable.placeholder).error(R.drawable.ic_img_error).into(Poster);
ImageView Screenshot = (ImageView)findViewById(R.id.ytThumb);
Glide.with(Screenshot.getContext()).load(screenshot).into(Screenshot);
ImageView img = (ImageView)findViewById(R.id.playButton);
img.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse(youtube));
startActivity(intent);
}
});
}
}
public boolean onCreateOptionsMenu(Menu menu) { // this does nothin
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.popup_dl_quality, menu);
menu.findItem(R.id.hd).setTitle(size720p);
menu.findItem(R.id.full_hd).setTitle(size1080p);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
onBackPressed();
return true;
}
}
popup_dl_quality.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="jaswinder.yts.app.MovieDetailsActivity"
app:theme="@style/ToolbarColoredBackArrow">
<item
android:id="@+id/hd"
android:title="720p"/>
<item
android:id="@+id/full_hd"
android:title="1080p"/>
</menu>
答案 0 :(得分:1)
@JaswinderSingh Have you passed the String with key value "size720p" and "size1080p" from the previous activity??