An example of the authoritative Android programming guide--Step 3--Data transfer between activities

On the previous basis, create a new activity to view the answer (cheating), which uses data transfer to the activity to pass the correct answer to the question from QuizeActivity to CheatActivity.
We use Intent to pass in values, before startActivity, we store the answers in intent as key-value pairs by intent's putExtra method, and then in cheatActivity we get the value by passing in the key value by intent's getExtra method.

  • First let's create CheatActivity, and here's the layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/warning_text"
        android:padding="24dp"
        />
<!--Show Answer textView-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        tools:text="answer"
        android:id="@+id/answer_text_view"/>
<!--click button You can see the answer-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/show_answer"
        android:id="@+id/show_answer_button"/>

</LinearLayout>
  • Similarly, we create a new button in the quizeActivity to jump to the cheatActivity interface
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cheat_button"
        android:id="@+id/cheat_button"/>
  • The next step is to pass values. First, we define the intent method in cheatActivity and store the values in the intent
//Key, mark the key to get the answer
private static final String EXTRA_ANSWER_IS_TRUE="com.apple.SummerDemo.answer_is_true";

Unified method:

//Store key-value
 public static Intent newIntent(Context context, boolean answerisTrue){
        Intent intent=new Intent(context,CheatActivity.class);//Jump Page
        intent.putExtra(EXTRA_ANSWER_IS_TRUE,answerisTrue);//Store values in intent, that is, the answer to the question, whether the question is correct or not
        return intent;
    }
  • Next, in the quizeActivity's cheatButton, jump, call the newIntent method in CheatActivity, pass in the answer to the question, and jump to the interface
  cheat_btn=(Button)findViewById(R.id.cheat_button);
        cheat_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean answerIsTrue=mQuestions[mCurrentIndex].isAnswerTrue();//Get the value of answerisTrue
                Intent intent=CheatActivity.newIntent(QuizeActivity.this,answerIsTrue);//transmit
                startActivity(intent);//Jump Page
                }
        });
  • When quizeActivity passes the data, we still need to get it in cheatActivity

Define Variables

private boolean answerisTrue;
private TextView answerTextView;
private Button show_answer;

The onCreate method is obtained by passing in the key, and false is the default second parameter in the method

answerisTrue=getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE,false);//Get the object forwarded by intent

Show the answer on the clean interface of cheatActivity

answerTextView=(TextView) findViewById(R.id.answer_text_view);
        show_answer=(Button)findViewById(R.id.show_answer_button);
        show_answer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //Show the answer
                if(answerisTrue){//If the answer is true, set to show true
                    answerTextView.setText(R.string.true_button);//true
                }else {
                    answerTextView.setText(R.string.false_button);//false
                }
            }
        });

Tags: Android xml encoding

Posted on Fri, 31 Jan 2020 20:46:37 -0500 by smerny