【发布时间】:2019-02-12 08:48:19
【问题描述】:
我想将我的活动更改为片段。我的代码如下。帮助将不胜感激。我花了两天时间没有任何成功。我已经修改了很多我上面的代码,但无法解决这个问题。最后五行让我很困惑。我还使用了一个 MediaPlayerPool 类,我认为它不会造成问题。
package com.example.soundpoolexample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private MediaPlayerPool mediaPlayerPool;
Button button;
Button button2;
class b1 implements OnTouchListener {
b1() {
}
public boolean onTouch(View view, MotionEvent motionEvent) {
view.setPressed(true);
if (motionEvent.getAction() == 0) {
MainActivity.this.mediaPlayerPool.play("android.resource://com.example.soundpoolexample/raw/a1");
return true;
} else if (motionEvent.getAction() != 1 && motionEvent.getAction() != 3) {
return false;
} else {
MainActivity.this.mediaPlayerPool.stop("android.resource://com.example.soundpoolexample/raw/a1");
return false;
}
}
}
class b2 implements OnTouchListener {
b2() {
}
public boolean onTouch(View view, MotionEvent motionEvent) {
view.setPressed(true);
if (motionEvent.getAction() == 0) {
MainActivity.this.mediaPlayerPool.play("android.resource://com.example.soundpoolexample/raw/a1");
return true;
} else if (motionEvent.getAction() != 1 && motionEvent.getAction() != 3) {
return false;
} else {
MainActivity.this.mediaPlayerPool.stop("android.resource://com.example.soundpoolexample/raw/a1");
return false;
}
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView((int) R.layout.activity_main);
this.button = (Button) findViewById(R.id.button);
this.button2 = (Button) findViewById(R.id.button2);
this.mediaPlayerPool = new MediaPlayerPool(this);
this.button.setOnTouchListener(new b1());
this.button2.setOnTouchListener(new b2());
}
}
【问题讨论】:
标签: java android android-fragments android-activity android-fragmentactivity