我正在開發一個應用程序,其中一個活動加載它時顯示滾動視圖中的文本文件(.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;
}
}
}
任何建議都與我如何能夠繼續完成這項任務有關,非常感謝。
在此先感謝。 (有人倒投了問題,我不介意,但至少在這之前指定原因)
編寫一個方法,您可以在其中傳遞文件名並動態地給它充氣 – Abx 2013-03-18 11:31:52
感謝回覆@Abhilash。我可以做到這一點,但我有很多文件。當我向後滑動時,如何保存最後文本的狀態,以及與之對應的swf和.mp3以便重新加載。 – 2013-03-18 11:34:28