我正在开发一个应用程序,当加载活动时,它会在滚动视图中显示一个文本文件(.txt)。就在文本文件下方,我有两个按钮(playAnimation 和 playAudio),按下时播放相应的文件(分别为 swf 和 mp3)。
现在我想修改当前代码以使其适用于多个文件(我可以通过多个活动来做同样的事情,但这将是多余的并且不是一种好的编程方式)。我有许多文本文件及其相应的动画和音频文件。我想使用相同的代码并且只动态更改文件名(即文本、动画和音频文件的文件名)
我的 MainActivity 如下(请参阅代码中的注释。这些是要进行更改的空间):
public class MainActivity extends Activity
{
Button playAnimationButton,playAudioButton;
MediaPlayer mp;
Context contextObject;
GestureDetector gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
setContentView(R.layout.activity_main);
ReadText readText=new ReadText(this);
TextView helloTxt = (TextView)findViewById(R.id.displaytext);
String fileName="textone";
// code to be written for getting the current page name and storing it in the String fileName
helloTxt.setText(readText.readTxt(fileName));
String audioName="pg3";
//Code to be written for getting the the current audioName
AssetFileDescriptor afd;
try {
afd=getAssets().openFd(audioName + ".mp3");
mp = new MediaPlayer();
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepareAsync();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
gestureDetector = new GestureDetector(this.getApplicationContext(),new MyGestureDetector());
View mainview = (View) findViewById(R.id.mainView);
// Set the touch listener for the main view to be our custom gesture listener
mainview.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return false;
}
return true;
}
});
playAnimationButton=(Button)findViewById(R.id.playanimation);
playAnimationButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.pause();
Intent startAnimation=new Intent(MainActivity.this,PlayAnimationActivity.class);
startAnimation.putExtra("SWF_NAME","a");
// code for sending the particular animation file corresponding to the current page
startActivity(startAnimation);
}
});
playAudioButton=(Button)findViewById(R.id.playaudio);
playAudioButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//code for dynamically sending the particular audio file associated with the current page
if(mp.isPlaying())
{
mp.pause();
}
else
{
mp.start();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class MyGestureDetector extends SimpleOnGestureListener
{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (Math.abs(e1.getY() - e2.getY()) > GlobalVariables.SWIPE_MAX_OFF_PATH) {
return false;
}
// right to left swipe
if(e1.getX() - e2.getX() > GlobalVariables.SWIPE_MIN_DISTANCE && Math.abs(velocityX) > GlobalVariables.SWIPE_THRESHOLD_VELOCITY) {
// The Code for loading the next text file with its corresponding audio and animation on buttons.
// left to right swipe
} else if (e2.getX() - e1.getX() > GlobalVariables.SWIPE_MIN_DISTANCE && Math.abs(velocityX) > GlobalVariables.SWIPE_THRESHOLD_VELOCITY) {
// The code for loading the previous text file with its corresponding audio and animation buttons.
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
}
}
非常感谢与我如何继续执行此任务有关的任何建议。
提前致谢。(有人否决了这个问题。我不介意。但至少在这样做之前说明原因)