Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使此代码更高效?_Java_Android_Arrays_Imagebutton_Android Videoview - Fatal编程技术网

Java 如何使此代码更高效?

Java 如何使此代码更高效?,java,android,arrays,imagebutton,android-videoview,Java,Android,Arrays,Imagebutton,Android Videoview,我正在制作一个Android应用程序,基本上只需按一下按钮就可以播放网站上的视频。从下面的代码可以看出,我有8个不同的按钮,可以播放8个不同的视频。我是编程新手,这一切都很好,但我知道这不是最好的编写方式 有没有办法用更少的代码行做同样的事情 package biz.slwdesign.tvlocallysouthdevon; import android.app.Activity; import android.content.pm.ActivityInfo; import android.

我正在制作一个Android应用程序,基本上只需按一下按钮就可以播放网站上的视频。从下面的代码可以看出,我有8个不同的按钮,可以播放8个不同的视频。我是编程新手,这一切都很好,但我知道这不是最好的编写方式

有没有办法用更少的代码行做同样的事情

package biz.slwdesign.tvlocallysouthdevon;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.VideoView;

public class Watch extends Activity implements OnClickListener {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
        setContentView(R.layout.watch); 

        //ImageButton1
        ImageButton video1 = (ImageButton) findViewById(R.id.ImageButton1);
        video1.setOnClickListener(this);

        //ImageButton2
        ImageButton video2 = (ImageButton) findViewById(R.id.ImageButton2);
        video2.setOnClickListener(this);

        //ImageButton3
        ImageButton video3 = (ImageButton) findViewById(R.id.ImageButton3);
        video3.setOnClickListener(this);

        //ImageButton4
        ImageButton video4 = (ImageButton) findViewById(R.id.ImageButton4);
        video4.setOnClickListener(this);

        //ImageButton5
        ImageButton video5 = (ImageButton) findViewById(R.id.ImageButton5);
        video5.setOnClickListener(this);

        //ImageButton6
        ImageButton video6 = (ImageButton) findViewById(R.id.ImageButton6);
        video6.setOnClickListener(this);

        //ImageButton7
        ImageButton video7 = (ImageButton) findViewById(R.id.ImageButton7);
        video7.setOnClickListener(this);

        //ImageButton7
        ImageButton video8 = (ImageButton) findViewById(R.id.ImageButton8);
        video8.setOnClickListener(this);

    }

    public void onClick(View v) {

        if(v.getId() == R.id.ImageButton1){
            setContentView(R.layout.watch);
            VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
            videoview1.setMediaController(new MediaController(this));
            videoview1.setVideoPath("http://slwdesign.biz/android/austins.mp4");
            videoview1.start();
            videoview1.requestFocus();
            videoview1.setOnCompletionListener(new OnCompletionListener() {           
                public void onCompletion(MediaPlayer videoview) {
                }
            });
        }
        if (v.getId() == R.id.ImageButton2){ 
            setContentView(R.layout.watch);
            VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
            videoview1.setMediaController(new MediaController(this));
            videoview1.setVideoPath("http://slwdesign.biz/android/brownsWigs.mp4");
            videoview1.start();
            videoview1.requestFocus();
        }
        if (v.getId() == R.id.ImageButton3){
            setContentView(R.layout.watch);
            VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
            videoview1.setMediaController(new MediaController(this));
            videoview1.setVideoPath("http://slwdesign.biz/android/frames&Boxes.mp4");
            videoview1.start();
            videoview1.requestFocus();
        }
        if (v.getId() == R.id.ImageButton4){
            setContentView(R.layout.watch);
            VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
            videoview1.setMediaController(new MediaController(this));
            videoview1.setVideoPath("http://slwdesign.biz/android/hatMckool.mp4");
            videoview1.start();
            videoview1.requestFocus();
        }
        if(v.getId() == R.id.ImageButton5){
            setContentView(R.layout.watch);
            VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
            videoview1.setMediaController(new MediaController(this));
            videoview1.setVideoPath("http://slwdesign.biz/android/gardenTime.mp4");
            videoview1.start();
            videoview1.requestFocus();
        }
        if (v.getId() == R.id.ImageButton6){
            setContentView(R.layout.watch);
            VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
            videoview1.setMediaController(new MediaController(this));
            videoview1.setVideoPath("http://slwdesign.biz/android/paulBarclay.mp4");
            videoview1.start();
            videoview1.requestFocus();
        }
        if (v.getId() == R.id.ImageButton7){
            setContentView(R.layout.watch);
            VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
            videoview1.setMediaController(new MediaController(this));
            videoview1.setVideoPath("http://slwdesign.biz/android/fishShed.mp4");
            videoview1.start();
            videoview1.requestFocus();
        }
        if(v.getId() == R.id.ImageButton8){
            setContentView(R.layout.watch);
            VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
            videoview1.setMediaController(new MediaController(this));
            videoview1.setVideoPath("http://slwdesign.biz/android/offBoutique.mp4");
            videoview1.start();
            videoview1.requestFocus();
        }
    }
}

例如,您可以通过以下方式缩短代码:

创建一个
HashMap
(可能是
static
),将
R.id.
链接到
videoPath
。然后在键集上迭代到
setOnClickListener
s,并在
onClick
中根据ID查找路径

编辑:那样怎么样

private static final LinkedHashMap<Integer, String> ID_MAP = new LinkedHashMap<Integer, String>();
static {
    ID_MAP.put(R.id.button1, "http://video1.avi");
    ID_MAP.put(R.id.button2, "http://video2.avi");
    ID_MAP.put(R.id.button3, "http://video3.avi");
    ID_MAP.put(R.id.button4, "http://video4.avi");
    ID_MAP.put(R.id.button5, "http://video5.avi");
}

private final Button[] mButtons = new Button[ID_MAP.size()];
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int i = 0;
    for (int id : ID_MAP.keySet()) {
        mButtons[i] = (Button) findViewById(id);
        mButtons[i].setOnClickListener(this);
        i++;
    }
}
@Override
public void onClick(View v) {
    int id = v.getId();
    String url = ID_MAP.get(id);
    // set url etc
}
private static final LinkedHashMap ID_MAP=new LinkedHashMap();
静止的{
ID\u地图放置(R.ID.button1,“http://video1.avi");
ID\u地图放置(R.ID.button2,“http://video2.avi");
ID\u地图放置(右ID按钮3,“http://video3.avi");
ID\u地图放置(右ID按钮4,“http://video4.avi");
ID\u地图放置(右ID按钮5,“http://video5.avi");
}
私有最终按钮[]mButtons=新按钮[ID_MAP.size()];
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
int i=0;
for(int-id:id\u-MAP.keySet()){
mButtons[i]=(按钮)findViewById(id);
mButtons[i].setOnClickListener(this);
i++;
}
}
@凌驾
公共void onClick(视图v){
int id=v.getId();
字符串url=ID\u MAP.get(ID);
//设置url等
}

您可以通过以下方式缩短代码:

创建一个
HashMap
(可能是
static
),将
R.id.
链接到
videoPath
。然后在键集上迭代到
setOnClickListener
s,并在
onClick
中根据ID查找路径

编辑:那样怎么样

private static final LinkedHashMap<Integer, String> ID_MAP = new LinkedHashMap<Integer, String>();
static {
    ID_MAP.put(R.id.button1, "http://video1.avi");
    ID_MAP.put(R.id.button2, "http://video2.avi");
    ID_MAP.put(R.id.button3, "http://video3.avi");
    ID_MAP.put(R.id.button4, "http://video4.avi");
    ID_MAP.put(R.id.button5, "http://video5.avi");
}

private final Button[] mButtons = new Button[ID_MAP.size()];
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int i = 0;
    for (int id : ID_MAP.keySet()) {
        mButtons[i] = (Button) findViewById(id);
        mButtons[i].setOnClickListener(this);
        i++;
    }
}
@Override
public void onClick(View v) {
    int id = v.getId();
    String url = ID_MAP.get(id);
    // set url etc
}
private static final LinkedHashMap ID_MAP=new LinkedHashMap();
静止的{
ID\u地图放置(R.ID.button1,“http://video1.avi");
ID\u地图放置(R.ID.button2,“http://video2.avi");
ID\u地图放置(右ID按钮3,“http://video3.avi");
ID\u地图放置(右ID按钮4,“http://video4.avi");
ID\u地图放置(右ID按钮5,“http://video5.avi");
}
私有最终按钮[]mButtons=新按钮[ID_MAP.size()];
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
int i=0;
for(int-id:id\u-MAP.keySet()){
mButtons[i]=(按钮)findViewById(id);
mButtons[i].setOnClickListener(this);
i++;
}
}
@凌驾
公共void onClick(视图v){
int id=v.getId();
字符串url=ID\u MAP.get(ID);
//设置url等
}

这样可以保存一些代码行

package biz.slwdesign.tvlocallysouthdevon;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.VideoView;

public class Watch extends Activity implements OnClickListener {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    setContentView(R.layout.watch);


    //ImageButton1
    ImageButton video1 = (ImageButton) findViewById(R.id.ImageButton1);
    video1.setOnClickListener(this);

    //ImageButton2
    ImageButton video2 = (ImageButton) findViewById(R.id.ImageButton2);
    video2.setOnClickListener(this);

    //ImageButton3
    ImageButton video3 = (ImageButton) findViewById(R.id.ImageButton3);
    video3.setOnClickListener(this);

    //ImageButton4
    ImageButton video4 = (ImageButton) findViewById(R.id.ImageButton4);
    video4.setOnClickListener(this);

    //ImageButton5
    ImageButton video5 = (ImageButton) findViewById(R.id.ImageButton5);
    video5.setOnClickListener(this);

    //ImageButton6
    ImageButton video6 = (ImageButton) findViewById(R.id.ImageButton6);
    video6.setOnClickListener(this);

    //ImageButton7
    ImageButton video7 = (ImageButton) findViewById(R.id.ImageButton7);
    video7.setOnClickListener(this);

    //ImageButton7
    ImageButton video8 = (ImageButton) findViewById(R.id.ImageButton8);
    video8.setOnClickListener(this);

}

public void onClick(View v) {

    setContentView(R.layout.watch);
    VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
    videoview1.setMediaController(new MediaController(this));
    if(v.getId() == R.id.ImageButton1){
    videoview1.setVideoPath("http://slwdesign.biz/android/austins.mp4");
    }
    if (v.getId() == R.id.ImageButton2){ 
    videoview1.setVideoPath("http://slwdesign.biz/android/brownsWigs.mp4");
    }
    if (v.getId() == R.id.ImageButton3){
    videoview1.setVideoPath("http://slwdesign.biz/android/frames&Boxes.mp4");
    }
    if (v.getId() == R.id.ImageButton4){
    videoview1.setVideoPath("http://slwdesign.biz/android/hatMckool.mp4");
    }
    if(v.getId() == R.id.ImageButton5){
    videoview1.setVideoPath("http://slwdesign.biz/android/gardenTime.mp4");
    }
    if (v.getId() == R.id.ImageButton6){
    videoview1.setVideoPath("http://slwdesign.biz/android/paulBarclay.mp4");
    }
    if (v.getId() == R.id.ImageButton7){
    videoview1.setVideoPath("http://slwdesign.biz/android/fishShed.mp4");
    }
    if(v.getId() == R.id.ImageButton8){
    videoview1.setVideoPath("http://slwdesign.biz/android/offBoutique.mp4");
    }
    videoview1.start();
    videoview1.requestFocus();
}

希望我没有忽略什么。

这样可以保存一些代码行

package biz.slwdesign.tvlocallysouthdevon;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.VideoView;

public class Watch extends Activity implements OnClickListener {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    setContentView(R.layout.watch);


    //ImageButton1
    ImageButton video1 = (ImageButton) findViewById(R.id.ImageButton1);
    video1.setOnClickListener(this);

    //ImageButton2
    ImageButton video2 = (ImageButton) findViewById(R.id.ImageButton2);
    video2.setOnClickListener(this);

    //ImageButton3
    ImageButton video3 = (ImageButton) findViewById(R.id.ImageButton3);
    video3.setOnClickListener(this);

    //ImageButton4
    ImageButton video4 = (ImageButton) findViewById(R.id.ImageButton4);
    video4.setOnClickListener(this);

    //ImageButton5
    ImageButton video5 = (ImageButton) findViewById(R.id.ImageButton5);
    video5.setOnClickListener(this);

    //ImageButton6
    ImageButton video6 = (ImageButton) findViewById(R.id.ImageButton6);
    video6.setOnClickListener(this);

    //ImageButton7
    ImageButton video7 = (ImageButton) findViewById(R.id.ImageButton7);
    video7.setOnClickListener(this);

    //ImageButton7
    ImageButton video8 = (ImageButton) findViewById(R.id.ImageButton8);
    video8.setOnClickListener(this);

}

public void onClick(View v) {

    setContentView(R.layout.watch);
    VideoView videoview1 = (VideoView) findViewById(R.id.videoView1);
    videoview1.setMediaController(new MediaController(this));
    if(v.getId() == R.id.ImageButton1){
    videoview1.setVideoPath("http://slwdesign.biz/android/austins.mp4");
    }
    if (v.getId() == R.id.ImageButton2){ 
    videoview1.setVideoPath("http://slwdesign.biz/android/brownsWigs.mp4");
    }
    if (v.getId() == R.id.ImageButton3){
    videoview1.setVideoPath("http://slwdesign.biz/android/frames&Boxes.mp4");
    }
    if (v.getId() == R.id.ImageButton4){
    videoview1.setVideoPath("http://slwdesign.biz/android/hatMckool.mp4");
    }
    if(v.getId() == R.id.ImageButton5){
    videoview1.setVideoPath("http://slwdesign.biz/android/gardenTime.mp4");
    }
    if (v.getId() == R.id.ImageButton6){
    videoview1.setVideoPath("http://slwdesign.biz/android/paulBarclay.mp4");
    }
    if (v.getId() == R.id.ImageButton7){
    videoview1.setVideoPath("http://slwdesign.biz/android/fishShed.mp4");
    }
    if(v.getId() == R.id.ImageButton8){
    videoview1.setVideoPath("http://slwdesign.biz/android/offBoutique.mp4");
    }
    videoview1.start();
    videoview1.requestFocus();
}

希望我没有忽略什么。

在xml文件中,设置此属性android:onClick=“onClick”而不是初始化ImageButton实例

在xml文件中,设置此属性android:onClick=“onClick”而不是初始化ImageButton实例

给出一些建议:

1.使用地图保存id和视频url

 //init the video url map in onCreate method
videoMap = new HashMap<Integer, String>();
videoMap.put(R.id.ImageButton1, "http://slwdesign.biz/android/austins.mp4");
...

//then init the onClickListener

Set<Integer> idSets = videoMap.keySet();
for(int id:idSets){
    ImageButton button = (ImageButton)findViewById(id);
    button.setOnClickListener(this);
}

//only init once
videoview1 = (VideoView) findViewById(R.id.videoView1);
提出一些建议:

1.使用地图保存id和视频url

 //init the video url map in onCreate method
videoMap = new HashMap<Integer, String>();
videoMap.put(R.id.ImageButton1, "http://slwdesign.biz/android/austins.mp4");
...

//then init the onClickListener

Set<Integer> idSets = videoMap.keySet();
for(int id:idSets){
    ImageButton button = (ImageButton)findViewById(id);
    button.setOnClickListener(this);
}

//only init once
videoview1 = (VideoView) findViewById(R.id.videoView1);

当然,最好的方法是在布局XML中定义单击时的
,您必须为每个按钮编写一个
On-Click
方法。这种方法对我来说比很多条件更清楚,然后永远不要编写两次或更多相同的代码行,将它们分组到一个方法中,然后从需要的位置调用该方法


我宁愿使用switch,case来表示您的代码类型,而不是if语句。关于。

当然,最好的方法是在布局XML中定义单击时的
方法,您必须为每个按钮编写一个
单击时的
方法。这种方法对我来说比很多条件更清楚,然后永远不要在一个方法中编写两行或更多相同的代码,将它们分组,然后从需要的位置调用该方法


我宁愿使用switch,case来表示您的代码类型,而不是if语句。关于。

在什么方面效率更高?我想你已经做对了:)Asker提到在代码行方面效率更高
您可以将各种URL存储在地图中,以减少代码量。我认为他想在什么方面更有效地消除代码重复?我想您已经做对了:)Asker提到了在代码行方面的效率。在
onClick(视图v)中
您可以将各种URL存储在地图中,以减少代码量。我认为他希望消除代码重复。如果他在布局xml文件中为ImageButtons编写onClick,那么也不需要for loop。如果他在布局xml文件中为ImageButtons编写onClick,那么也不需要for loop。上面的评论是对我来说最有用的是因为我快要死了。唯一错误的是需要取出mediaplayer导入,因为它们现在不需要了,并且需要添加一个大括号},并且需要添加结尾以结束Watch类。非常感谢:-)上面的评论对我来说是最有用的,因为我已经过期了。唯一错误的是需要取出mediaplayer导入,因为它们现在不需要了,并且需要添加一个大括号},并且需要添加结尾以结束Watch类。非常感谢:-)