在Textview中显示结果

时间:2017-03-01 18:45:14

标签: java android android-studio bundle textview

我正在创建一个简单的应用程序,用户可以在其中获得问题并选择答案(radiobuttons),并在下一个活动中显示他们的分数,问题是它没有显示正确的分数..

我的quiz_layout中有一些问题和答案:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >
        <ImageView
            android:id="@+id/yourId"
            android:src="@mipmap/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="4dp"
            android:layout_gravity="center" />
    </android.support.v7.widget.Toolbar>
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/toolbar1">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp"
        android:id="@+id/linearLayout">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1. HTTP je skraćenica za:"
            android:textColor="@color/colorPrimary"
            android:textSize="15sp"/>


        <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <RadioButton android:id="@+id/radio_HyperTextTransferProvider"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hyper Text Transfer Provider"
                android:onClick="fourthRadioButtons"/>
            <RadioButton android:id="@+id/radio_HyperTranslateTransferProtocol"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hyper Translate Transfer Protocol"
                android:onClick="fourthRadioButtons"/>
            <RadioButton android:id="@+id/radio_HyperTextTransferProtocol"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hyper Text Transfer Protocol"
                android:onClick="fourthRadioButtons"/>
            <RadioButton android:id="@+id/radio_dontknow4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Ne znam"
                android:onClick="fourthRadioButtons"/>
        </RadioGroup>
        <View
            android:layout_width="0dp"
            android:layout_height="5dp"
            android:layout_weight="0" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2. FTP je protokol koji omogućuje:"
            android:textColor="@color/colorPrimary"
            android:textSize="15sp"/>


        <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <RadioButton android:id="@+id/radio_morespeed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="veću brzinu download-a velikih datoteka"
                android:onClick="fifthRadioButtons"/>
            <RadioButton android:id="@+id/radio_transferdata"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="prenos datoteka među računarima na internetu"
                android:onClick="fifthRadioButtons"/>
            <RadioButton android:id="@+id/radio_controlcomputer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="kontrolu računara putem interneta "
                android:onClick="fifthRadioButtons"/>
            <RadioButton android:id="@+id/radio_dontknow5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Ne znam"
                android:onClick="fifthRadioButtons"/>
        </RadioGroup>




        <View
            android:layout_width="0dp"
            android:layout_height="5dp"
            android:layout_weight="0" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3. Program za crtanje koji dolazi u sklopu Windows operativnog sistema naziva se:"
        android:textColor="@color/colorPrimary"
        android:textSize="15sp"/>


    <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton android:id="@+id/radio_paint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Paint"
            android:onClick="firstRadioButtons"/>
        <RadioButton android:id="@+id/radio_photoshop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Photoshop"
            android:onClick="firstRadioButtons"/>
        <RadioButton android:id="@+id/radio_coreldraw"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CorelDraw"
            android:onClick="firstRadioButtons"/>
        <RadioButton android:id="@+id/radio_dontknow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ne znam"
            android:onClick="firstRadioButtons"/>
    </RadioGroup>
        <View
            android:layout_width="0dp"
            android:layout_height="5dp"
            android:layout_weight="0" />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4. Kako se nazivaju programi koje korsitimo da bi zaštitili računar od virusa?"
            android:textColor="@color/colorPrimary"
            android:textSize="15sp"/>


        <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <RadioButton android:id="@+id/radio_driveri"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Drajveri"
                android:onClick="secondRadioButtons"/>
            <RadioButton android:id="@+id/radio_antivirusi"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Antivirusi"
                android:onClick="secondRadioButtons"/>
            <RadioButton android:id="@+id/radio_editori"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Editori"
                android:onClick="secondRadioButtons"/>
            <RadioButton android:id="@+id/radio_dontknow2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Ne znam"
                android:onClick="secondRadioButtons"/>
        </RadioGroup>



        <View
            android:layout_width="0dp"
            android:layout_height="5dp"
            android:layout_weight="0" />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5. Skraćenica WWW označava:"
            android:textColor="@color/colorPrimary"
            android:textSize="15sp"/>


        <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <RadioButton android:id="@+id/radio_wordwindowweb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Word Window Web"
                android:onClick="thirdRadioButtons"/>
            <RadioButton android:id="@+id/radio_worldwideweb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="World Wide Web"
                android:onClick="thirdRadioButtons"/>
            <RadioButton android:id="@+id/radio_worldwidthweb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="World Width Web"
                android:onClick="thirdRadioButtons"/>
            <RadioButton android:id="@+id/radio_dontknow3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Ne znam"
                android:onClick="thirdRadioButtons"/>
        </RadioGroup>
        <View
            android:layout_width="0dp"
            android:layout_height="5dp"
            android:layout_weight="0" />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6. Prva računarska mreža se nazivala:"
            android:textColor="@color/colorPrimary"
            android:textSize="15sp"/>


        <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <RadioButton android:id="@+id/radio_arpanet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ARPANET"
                android:onClick="sixthRadioButtons"/>
            <RadioButton android:id="@+id/radio_lannet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="LANNET"
                android:onClick="sixthRadioButtons"/>
            <RadioButton android:id="@+id/radio_adslnet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ADSLNET"
                android:onClick="sixthRadioButtons"/>
            <RadioButton android:id="@+id/radio_dontknow6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Ne znam"
                android:onClick="sixthRadioButtons"/>
        </RadioGroup>

        <View
            android:layout_width="0dp"
            android:layout_height="5dp"
            android:layout_weight="0" />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7. AutoCAD je vrsta programa koja služi za:"
            android:textColor="@color/colorPrimary"
            android:textSize="15sp"/>


        <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <RadioButton android:id="@+id/radio_uredjivanje"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Uređivanje fotografija"
                android:onClick="seventhRadioButtons"/>
            <RadioButton android:id="@+id/radio_crtanje"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Crtanje likovnih crteža"
                android:onClick="seventhRadioButtons"/>
            <RadioButton android:id="@+id/radio_projektovanje"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tehničko projektovanje"
                android:onClick="seventhRadioButtons"/>
            <RadioButton android:id="@+id/radio_dontknow7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Ne znam"
                android:onClick="seventhRadioButtons"/>
        </RadioGroup>

        <View
            android:layout_width="0dp"
            android:layout_height="5dp"
            android:layout_weight="0" />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8. Mrežna struktura na slici je:"
            android:textColor="@color/colorPrimary"
            android:textSize="15sp"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/mrezna"
            android:id="@+id/imageView7"
            android:layout_gravity="left"/>

        <View
            android:layout_width="0dp"
            android:layout_height="5dp"
            android:layout_weight="0" />

        <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <RadioButton android:id="@+id/radio_razgranata"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Razgranata"
                android:onClick="eightRadioButtons"/>
            <RadioButton android:id="@+id/radio_magistrala"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Magistrala"
                android:onClick="eightRadioButtons"/>
            <RadioButton android:id="@+id/radio_pravolinijska"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Pravolinijska"
                android:onClick="eightRadioButtons"/>
            <RadioButton android:id="@+id/radio_dontknow8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Ne znam"
                android:onClick="eightRadioButtons"/>
        </RadioGroup>




        <Button
            android:text="Potvrdi"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginRight="21dp"
            android:layout_marginEnd="21dp"
            android:layout_marginBottom="44dp"
            android:id="@+id/button7"
            android:onClick="nextButton"
            android:background="@drawable/button_selector"/>


    </LinearLayout>

</ScrollView>



</RelativeLayout>

这是我的quiz_activity,我在那里计算我的观点:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RadioButton;



public class quiz_activity extends AppCompatActivity {
    int points = 0;
    Toolbar toolbar;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quiz_layout);
        toolbar = (Toolbar) findViewById(R.id.toolbar1);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Test");
        getSupportActionBar().setSubtitle("Osnovne škole");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }


    public void firstRadioButtons(View view) {

        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();


        // Check which radio button was clicked
        switch (view.getId()) {


            case R.id.radio_paint:
                if (checked)
                    points += 2;
                break;
            case R.id.radio_photoshop:
                if (checked)
                    --points;
                break;
            case R.id.radio_coreldraw:
                if (checked)
                    --points;

                break;
        }

    }
    public void secondRadioButtons(View view) {

        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.radio_driveri:
                if (checked)
                    --points;
                break;
            case R.id.radio_editori:
                if (checked)
                    --points;
                break;
            case R.id.radio_antivirusi:
                if (checked)
                    points += 2;
                break;
        }

    }
    public void thirdRadioButtons(View view) {

        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.radio_wordwindowweb:
                if (checked)
                    --points;
                break;
            case R.id.radio_worldwideweb:
                if (checked)
                    points += 2;
                break;
            case R.id.radio_worldwidthweb:
                if (checked)
                    --points;
                break;
        }

    }
    public void fourthRadioButtons(View view) {

        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.radio_HyperTextTransferProvider:
                if (checked)
                    --points;
                break;
            case R.id.radio_HyperTranslateTransferProtocol:
                if (checked)
                    --points;
                break;
            case R.id.radio_HyperTextTransferProtocol:
                if (checked)
                    points += 2;
                break;
        }

    }
    public void fifthRadioButtons(View view) {

        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.radio_morespeed:
                if (checked)
                    --points;
                break;
            case R.id.radio_transferdata:
                if (checked)
                    points += 2;
                break;
            case R.id.radio_controlcomputer:
                if (checked)
                    --points;
                break;
        }

    }
    public void sixthRadioButtons(View view) {

        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.radio_arpanet:
                if (checked)
                    points = +2;
                break;
            case R.id.radio_lannet:
                if (checked)
                    --points;
                break;
            case R.id.radio_adslnet:
                if (checked)
                    --points;
                break;
        }

    }
    public void seventhRadioButtons(View view) {

        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.radio_uredjivanje:
                if (checked)
                    --points;
                break;
            case R.id.radio_crtanje:
                if (checked)
                    --points;
                break;
            case R.id.radio_projektovanje:
                if (checked)
                    points += 2;
                break;
        }

    }
    public void eightRadioButtons(View view) {

        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.radio_razgranata:
                if (checked)
                    --points;
                break;
            case R.id.radio_magistrala:
                if (checked)
                    points += 2;
                break;
            case R.id.radio_pravolinijska:
                if (checked)
                    --points;
                break;
        }

    }
    public void nextButton(View view)
    {
        Intent intent = new Intent(quiz_activity.this, score_activity.class);
        intent.putExtra("some_key", points);
        startActivity(intent);
    }

    }

当用户回答所有问题时,它说他们有6分,而他们实际上有16分。

以下是应该显示的score_layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">



    <LinearLayout

        android:layout_height="match_parent"
        android:layout_width="fill_parent"
        android:layout_marginBottom="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="20dp"
        android:layout_below="@id/toolbar1"
        android:orientation="vertical"
        android:gravity="center_vertical">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Vaš rezultat na ovom testu je:"
                android:textSize="25sp"
                android:layout_gravity="center_vertical"/>


        </LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_gravity="center_horizontal"
    android:orientation="horizontal">
    <TextView
        android:text="0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/score"
        android:textSize="25sp"
        android:gravity="center"
        android:textColor="@color/colorAccent"/>

    <TextView
        android:text="/16"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/score1"
        android:textSize="25sp"
        android:gravity="center"/>



</LinearLayout>


    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/all_rights"
        android:id="@+id/textView"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

还有score_activity我从Quiz_activity发送Bundle并在我的TextView中显示它:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;



public class score_activity extends AppCompatActivity {


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.score_layout);

        Bundle bundle = getIntent().getExtras();
        int points = bundle.getInt("some_key");

        TextView scoreTextView = (TextView) findViewById(R.id.score);
        scoreTextView.setText(String.valueOf(points));
    }
}

我尝试了很多东西,但没有任何效果......

0 个答案:

没有答案