-2

簡単な Android プログラムを作成しました。

機能: 起動時に、テキストとボタンが表示されます。ボタンには、触れるたびに増加する数字があります。ボタン上の 2 から 10 までの乱数で、画像が表示され、soundclip が表示されます。

これがその機能です。画像が表示されたらアプリは終了です。

今やりたいことは、アプリの起動ページに戻るタッチ機能を作成することです。そのためには、あなたの助けが必要です。私は解決策をインターネットで検索しようとしましたが、これは初心者なので、コードに実装する方法がわかりません。

これが私のコードです:

package net.ibasic;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HelloAndroidActivity extends Activity implements OnClickListener {
private int i = 0;
public int low = 2;
public int high = 10;
public int num = low + (int) ( Math.random()*(high -low) + 0.5 );
MediaPlayer mpAudio;
MediaPlayer mpAudio1;
/** Called when the activity is first created. */ 
@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);
    //creating the setContentView by java-code instead of Xml
    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setOrientation(LinearLayout.VERTICAL);

    TextView textView = new TextView(this);
    textView.setText(R.string.hello);

    linearLayout.addView(textView);

    //creating a button for the app
    Button button = new Button(this);
    linearLayout.addView(button);

    setContentView(linearLayout);
    update(button);

    //adding buttonListener
    button.setOnClickListener(this);
    mpAudio = MediaPlayer.create(this, R.raw.kitten);
    mpAudio1 = MediaPlayer.create(this, R.raw.scary);
}



private void update(Button button) {
    button.setText("Amount of Clicks " + i++);

}

public void onClick(View button) {
    update((Button)button);

    if (i==num){

        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        if (num % 2 == 0){
        mpAudio.start();
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(R.drawable.kitten);

        TextView textView1 = new TextView(this);
        textView1.setText(R.string.kittentext);
        linearLayout.addView(textView1);
        linearLayout.addView(imageView);

        }
        else{
            mpAudio1.start();
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.drawable.scary);

            TextView textView1 = new TextView(this);
            textView1.setText(R.string.scarytext);
            linearLayout.addView(textView1);
            linearLayout.addView(imageView);

        }




        setContentView(linearLayout);
    }
}
}
4

1 に答える 1

2

完了したら、次のコードを使用してアクティビティを再開します。

Intent intent = getIntent();
finish();
startActivity(intent); //

参照


ボタンのクリックでこれを行うことができます/performClick()ボタンオブジェクトのメソッドを使用して、またはいつでもボタンのクリックを自動化することもできます。

于 2012-05-28T22:01:29.390 に答える