0

很抱歉再次出现这个按钮,我想在这里找出正确的位置。我只希望它在这个类上只在按下后退出应用程序,但我无法弄清楚到底要写什么或在哪里写。谢谢您的帮助。下面的类代码。包 com.example.whattodo2;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class Title extends Activity {
Button reset, rts;
ImageView title;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_title);
    reset = (Button) findViewById(R.id.reset);
    reset.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                       double rand = Math.random();
                       if(rand < 0.5){
                           Intent reset1 = new Intent(Title.this, MainActivity.class);
                           startActivity(reset1);
                       } else {
                           Intent reset2 = new Intent(Title.this, Question36.class);
                           startActivity(reset2);
                       }
                }
            });
    rts = (Button) findViewById(R.id.rts);
    rts.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent rts=new Intent(Title.this,Rts.class);
                    startActivity(rts);

                }
            });

         final Animation a = AnimationUtils.loadAnimation(this, R.animator.animation);
         a.reset();
         final ImageView rImage = (ImageView) findViewById(R.id.title);

         RelativeLayout layout = (RelativeLayout) findViewById(R.id.root);
         layout.setOnClickListener(new OnClickListener() {

             @Override public void onClick(View v) {
         rImage.startAnimation(a);
         func(); //A call to the function.

         }
         });


}

protected void func() {
    // TODO Auto-generated method stub

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.title, menu);
    return true;
}

}
4

2 回答 2

2

像这样使用:

public class Title extends Activity {

@Override
public void onBackPressed() {
// TODO Auto-generated method stub

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

super.onBackPressed();
}
}

希望这会给你一些解决方案。

于 2013-08-05T13:27:36.597 回答
1

在所有活动中的 onCreate 之前:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {

        Intent intent=new Intent (Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

            // finish();
    }
    return true;
}
于 2013-08-05T13:30:03.000 回答