从一项活动转移到另一项活动

时间:2019-02-28 18:32:01

标签: android

此代码位于我的playAgain中,并且我创建了另一个名为activity的活动。当时间结束时,我希望转到我的主public void playAgain(){ score = 0; numberOfQuestions = 0; timerTextView.setText("30s"); pointsTextView.setText("0/0"); resultTextView.setText(""); new CountDownTimer(30100, 1000) { @Override public void onTick(long millisUntilFinished) { timerTextView.setText(String.valueOf((millisUntilFinished)/1000) + "s"); } @Override public void onFinish() { timerTextView.setText("0s"); String getFinalScore = Integer.toString(score) + "/" + Integer.toString(numberOfQuestions); Intent intent = new Intent(getApplicationContext(), PlayAgain.class); //Create the bundle Bundle bundle = new Bundle(); //Add your data to bundle bundle.putString("points", getFinalScore); //Add the bundle to the intent intent.putExtras(bundle); startActivity(intent); //playAgainButton.setVisibility(View.VISIBLE); // resultTextView.setText("Your Score: " + Integer.toString(score) + "/" + Integer.toString(numberOfQuestions)); } }.start(); 进行“再次播放”活动,显示我的最终得分并再次播放按钮,当我单击“再次播放”按钮时,它应该转到主要活动并将时间设置回30秒并得分为0/0。

//这是我mainActivity中的再次播放代码

 Button playAgainButton;
    TextView finalTextView;

public void playAgain(View view) {


    //Get the bundle
    Bundle bundle = getIntent().getExtras();

    //Extract the data…
    String getFinalScore = bundle.getString("points");

    finalTextView.setText(getFinalScore);

    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(intent);
}

}

//这是我playAgainActicity中的代码

<!DOCTYPE html>
<html>
    <head>
        <title>Page title</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <script src="<%out.print(request.getContextPath());%>/jquery/external/jquery/jquery.js"></script> 

        <script src="<%out.print(request.getContextPath());%>/jquery/jquery-ui.js"></script>
        <link rel="STYLESHEET" type="text/css" href="<%out.print(request.getContextPath());%>/jquery/jquery-ui.css"></link>

        <link href="<%out.print(request.getContextPath());%>/select2/4.0.6-rc.0/css/select2.min.css?v=2" rel="stylesheet" />
        <script src="<%out.print(request.getContextPath());%>/select2/4.0.6-rc.0/js/select2.min.js?v=2"></script>

        <script src="<%out.print(request.getContextPath());%>/select2/maximize-select2-height/maximize-select2-height.js"></script>
        <script src="<%out.print(request.getContextPath());%>/select2/maximize-select2-height/maximize-select2-height.min.js"></script>

        <link rel="STYLESHEET" type="text/css" href="<%out.print(request.getContextPath());%>/font-awesome/css/font-awesome.min.css">

        <script type="text/javascript">
            $(document).ready(function () {
                $('.form-control').select2({
                    theme: "classic",
                    templateSelection: function (data, container) {
                        if (data.id == '') {
                            $(container).css("color", "grey");
                        } else {
                            $(container).css("color", "black");
                        }
                        return data.text;
                    },
                }).maximizeSelect2Height();
               
            });
        </script>
    </head>
    <body>

        <div>
            <div class ="col-sm-3">
                <label for="app1">Name:</label>
            </div>
            <div class ="col-sm-9">                        
                <select required class = "form-control" id ="app1">
                    <option value="" >select a program name</option>
                    <option >india</option>
                    <option >china</option>
                    <option>netherlands</option>
                    <option >africa</option>
                    <option >nepal</option>
                    <option >india</option>
                    <option >china</option>
                    <option>netherlands</option>
                    <option >africa</option>
                    <option >nepal</option>
                    <option >india</option>
                    <option >china</option>
                    <option>netherlands</option>
                    <option >africa</option>
                    <option >nepal</option>
                    
                </select>             
            </div>
        </div>

    </body>
</html>

3 个答案:

答案 0 :(得分:2)

在您的playAgain活动中,将此行放入onCreate方法中

//Get the bundle
Bundle bundle = getIntent().getExtras();

//Extract the data…
String getFinalScore = bundle.getString("points");

finalTextView.setText(getFinalScore);

在按钮的点击事件上调用playAgain()

playAgainButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        playAgain();
    }
});


public void playAgain() {
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(intent);
}

答案 1 :(得分:0)

移动此代码:

//Get the bundle
    Bundle bundle = getIntent().getExtras();

    //Extract the data…
    String getFinalScore = bundle.getString("points");

    finalTextView.setText(getFinalScore);

到playAgainActicity的onCreate方法。 然后在playAgain方法中添加finishAffinity

public void playAgain(View view) {

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(intent);
    finishAffinity();
}

答案 2 :(得分:0)

MainActivity的代码看起来不错

请如下更改PlayAgain活动的代码;并且不要忘记将PlayAgain添加到清单文件中,并使用android:onClick="playAgain"作为按钮。

public class PlayAgain extends AppCompatActivity {

    TextView finalTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_again);

        //Get the bundle
        Bundle bundle = getIntent().getExtras();

        //Extract the data…
        String getFinalScore = bundle.getString("points");
        finalTextView = findViewById(R.id.finalTextView);

        finalTextView.setText(getFinalScore);

    }


    public void playAgain(View view) {

        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(intent);
    }
}

希望有帮助!