[英]How can I make this code more efficient?
I'm making a Android app that basically plays video from a website on the press of a button.我正在制作一个 Android 应用程序,基本上只需按一下按钮即可播放来自网站的视频。 As you can see by the code bellow I have 8 different buttons that will play eight different videos.
正如您在下面的代码中看到的那样,我有 8 个不同的按钮,可以播放 8 个不同的视频。 I'm new to programming and this all works fine but I know it's not the best way to write it.
我是编程新手,一切正常,但我知道这不是编写它的最佳方式。
Is there a way I can do the same thing with less lines of code?有没有一种方法可以用更少的代码行来做同样的事情?
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();
}
}
}
in xml file,set this attribute android:onClick="onClick" instead of initialize ImageButton instance在 xml 文件中,设置此属性 android:onClick="onClick" 而不是初始化 ImageButton 实例
give some advice:给点建议:
1.use Map to save the id and video url. 1.使用Map保存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);
also, if you in the xml, set the android:onClick="onClick"
, the loop onClickListener
init can be cancel.另外,如果你在 xml 中设置
android:onClick="onClick"
,可以取消onClickListener
init 的循环。
2.onClick method: 2.onClick方法:
//onclick
String url = videoMap.get(v.getId());
if(url!=null){
videoview1.setMediaController(new MediaController(this));
videoview1.setVideoPath(url);
videoview1.start();
videoview1.requestFocus();
}
You can shorten your code for example that way:例如,您可以这样缩短代码:
Create a HashMap<Integer, String>
(probably static
) that links the R.id.*
to the videoPath
.创建一个将
R.id.*
链接到videoPath
的HashMap<Integer, String>
(可能是static
)。 Then iterate over the keyset to setOnClickListener
s and in onClick
do a lookup of the path based on the ID.然后将键集迭代到
setOnClickListener
,并在onClick
中根据 ID 查找路径。
Edit: about that way编辑:关于那种方式
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
}
You can save some lines of code this way.您可以通过这种方式节省一些代码行。
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();
}
Hope I haven't overlooked something.希望我没有忽略任何事情。
Definitely the best way is to define the On Click
in the layout XML, you have to write an On Click
method for every button this way for me is more clear than the lot of conditional, then never write the same lines of code twice or more group them in a method, then call the method from where is needed.绝对最好的方法是在布局 XML 中定义
On Click
Click 方法,你必须为每个按钮编写一个On Click
方法,这种方式对我来说比很多条件更清晰,然后永远不要写相同的代码行两次或更多次将它们分组在一个方法中,然后从需要的地方调用该方法。
I´d rather use switch, case for your type of code instead of the if statment.我宁愿为您的代码类型使用 switch, case 而不是 if 语句。 Regards.
问候。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.