2013-03-18 46 views
-1

我正在開發一個應用程序,其中一個活動加載它時顯示滾動視圖中的文本文件(.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; 
     } 



    } 

} 

任何建議都與我如何能夠繼續完成這項任務有關,非常感謝。

在此先感謝。 (有人倒投了問題,我不介意,但至少在這之前指定原因)

+1

編寫一個方法,您可以在其中傳遞文件名並動態地給它充氣 – Abx 2013-03-18 11:31:52

+0

感謝回覆@Abhilash。我可以做到這一點,但我有很多文件。當我向後滑動時,如何保存最後文本的狀態,以及與之對應的swf和.mp3以便重新加載。 – 2013-03-18 11:34:28

回答

1

我想出了一個辦法,最終做到這一點。這是在我的情況下工作的代碼。我有一個類,它根據計數號維護文件的名稱。根據以下方法的計數動態發送文件名。下面的方法動態讀取該文件並返回該字符串,並在刷卡時將其設置爲TextView。向左和向右滑動,分別增加和減少計數。爲動畫和聲音文件保留類似的代碼。這工作順利。

public String readTxt(String fileName) 
     { 


      try { 
       InputStream is; 
       is = context.getAssets().open(fileName + ".txt"); 
       ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

       int i; 
       i = is.read(); 
       while (i != -1) 
       { 
        byteArrayOutputStream.write(i); 
        i = is.read(); 
       } 

       is.close(); 

       return byteArrayOutputStream.toString(); 

      } 

      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 

      return fileName; 
     } 

更好和更有效的解決方案是值得歡迎的。現在我接受這個答案。感謝所有的答覆。

1

嘗試使用ListView列出所有文件名並激活onItemclick監聽器。從覆蓋onItemclick你可以從列表中獲取文件名和數組的位置。請參考http://www.vogella.com/articles/AndroidListView/article.html的下列ListView代碼

+0

感謝@Senthil的回覆。但您分享的網頁不可用。但我會盡力做到這一點。謝謝。 – 2013-03-18 11:37:10

+0

@abhi_is_learning_android嘗試上面的鏈接..我編輯我的答案 – 2013-03-18 11:41:12

2

在活動中使用片段,您可以在同一個片段上加載多個文件。檢查下面的一小段代碼,這將在你的Activity的onCreate()加載片段。

Fragment newFragment = new YourFragment; //Instance of your fragment 
FragmentTransaction ft = getSupportFragmentManager() 
       .beginTransaction(); 
ft.add(R.id.fragment_container, newFragment).commit(); 

以下代碼用於加載新的片段添加片段到堆棧。

void addFragmentToStack(String url) { 

    mFragmentUrl = url; 
    // Instantiate a new fragment. 
    Fragment newFragment = new your fagment; 
    // Add the fragment to the activity, pushing this transaction 
    // on to the back stack. 
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    ft.replace(R.id.fragment_container, newFragment); 
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
    ft.addToBackStack(null); 
    ft.commit(); 

}