简体   繁体   English

需要帮助使用android菜单

[英]Need help working with android menu

Android Studio newbie and I can't find anything that makes sense to me to get the menu to work the way that I want. Android Studio新手,我找不到让菜单按我想要的方式工作对我有意义的任何东西。

My app streams videos from my website to the device. 我的应用将视频从我的网站流式传输到设备。 The menu has a list of 3 videos to watch. 菜单中列出了3个要观看的视频。 I would like for users to be able to open the menu, select the video they want to watch and then have that video load into the player. 我希望用户能够打开菜单,选择他们要观看的视频,然后将该视频加载到播放器中。 I have everything working, except that when you click on a video from the menu nothing happens and I know that this is because I have no code for it in my MainActivity.java file. 我一切正常,除了单击菜单上的视频时什么都没有发生,我知道这是因为在MainActivity.java文件中没有该代码。 Here's what MainActivity.java looks like: 这是MainActivity.java样子:

package com.mywebsite.videostreamer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.net.Uri;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    VideoView vidView = (VideoView) findViewById(R.id.myVideo);
    String vidAddress = "http://www.mywebsite.com/vids/vidOne.mp4";
    Uri vidUri = Uri.parse(vidAddress);
    vidView.setVideoURI(vidUri);
    MediaController vidControl = new MediaController(this);
    vidControl.setAnchorView(vidView);
    vidView.setMediaController(vidControl);
    vidView.start();
    vidView.seekTo(100);
    vidView.pause();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, 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();
    switch (item.getItemId()) {

        case R.id.vidOne:
            return true;

        case R.id.vidTwo:
            return true;

        case R.id.vidThree:
            return true;
    }
        return super.onOptionsItemSelected(item);
    }

public void showPopup(MenuItem item) {
}
}

With this and the code in my menu_main.xml file, I'm able to launch the app, click the menu button and view the list of available videos and then of course, when I click a video, nothing happens because I don't have the code to tell it what to do. 使用此代码和menu_main.xml文件中的代码,我可以启动该应用程序,单击菜单按钮并查看可用视频的列表,然后当然,当我单击视频时,什么也没发生,因为我没有有代码告诉它该怎么做。

I'm assuming that I will need to create another instance of @Override and have the int id = item.getItemId(); 我假设我需要创建的另一个实例@Override ,并有int id = item.getItemId(); , the id variable that contains the selection, to put together the url string + the id variable. ,即包含选择的id变量,以将url字符串+ id变量放在一起。 So something like String vidAddress = "http://www.mywebsite.com/vids/ + id; and that will be contained in the new @Override section and will handle loading the new video once selected. 因此,类似String vidAddress = "http://www.mywebsite.com/vids/ + id;内容将包含在新的@Override部分中,并在选定后处理新视频的加载。

Am I in the ballpark? 我在球场上吗? Can someone help me find my seat? 有人可以帮我找到我的位子吗?

Firstly, its worth saying that the '@override' is not an object or thing you create instances of, just in case this is what you meant. 首先,值得一提的是,“ @ override”不是您创建实例的对象或事物,以防万一,这就是您的意思。

What it means is that the super class, ie the class that is being subclassed, has a method of the same name and this method is being 'over written' in the current class. 这意味着超类(即被子类化的类)具有相同名称的方法,并且该方法在当前类中被“覆盖”。

So for your case above, the super class 'AppCompatActivity' already has a method 'onOptionsItemSelected' (for example) and this is being over written in your 'MainActivity' class. 因此,对于上述情况,超级类“ AppCompatActivity”已经具有方法“ onOptionsItemSelected”(例如),并且该类已被覆盖在“ MainActivity”类中。

For you specific question, there are different approaches you could take, but if you want to check it is working quickly you can simply change the video directly in the 'onOptionsItemSelected' code. 对于您的特定问题,可以采用不同的方法,但是如果要检查它是否正在快速运行,则可以直接在“ onOptionsItemSelected”代码中直接更改视频。 In other words, assuming that the video playback code in your onCreate works, the following should change the video when a user clicks the first option: 换句话说,假设onCreate中的视频播放代码有效,则当用户单击第一个选项时,以下内容应更改视频:

public class MainActivity extends AppCompatActivity {

private VideoView vidView;
.
.
@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();
    switch (item.getItemId()) {

        case R.id.vidOne: {
            vidView.stopPlayback();
            videoUri = Uri.parse("http://www.mywebsite.com/vids/NewVideo.mp4");
            vidView.setVideoURI(videoUri);
            vidView.start();
            return true;

        case R.id.vidTwo:
            return true;

        case R.id.vidThree:
            return true;
    }
        return super.onOptionsItemSelected(item);
    }

Note the definition of vidView is taken outside onCreate so you can use it elsewhere also. 注意vidView的定义在onCreate之外,因此您也可以在其他地方使用它。

You don't necessarily want to do the actual work within the call back itself always, but trying the above will at least give you a feel for how it works and you can decide if you want to modify it then. 您不一定要始终在回叫本身中进行实际工作,但是尝试上述操作至少可以使您对它的工作方式有一定的了解,然后可以决定是否要对其进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM