-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;
        }



    }

}

非常感谢与我如何继续执行此任务有关的任何建议。

提前致谢。(有人否决了这个问题。我不介意。但至少在这样做之前说明原因)

4

3 回答 3

2

在活动中使用片段,您可以在同一个片段上加载尽可能多的文件。检查以下一小段代码,这将在您onCreate()的 Activity 中加载该片段。

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();

}
于 2013-03-18T11:35:45.410 回答
1

尝试使用 ListView 列出所有文件名并启动 onItemclick 侦听器。从 Override onItemclick 您可以从列表中获取文件名及其数组位置。请参阅http://www.vogella.com/articles/AndroidListView/article.html中的以下 ListView 代码

于 2013-03-18T11:32:05.460 回答
1

我终于想出了一个办法。这是适用于我的情况的代码。我有一个根据计数维护文件名称的类。文件名根据计数动态发送到以下方法。下面的方法动态读取该文件并返回字符串并将其设置为TextViewon swipe。左右滑动,分别递增和递减计数。为动画和声音文件维护了类似的代码。这工作顺利。

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;
        }

欢迎更好、更有效的解决方案。现在我接受这个答案。谢谢大家的回复。

于 2013-04-16T11:40:07.470 回答