我有一个按钮,可以将我的积分发送给另一个活动:
public void nextButton(View view)
{
Intent intent = new Intent(quiz_activity.this, score_activity.class);
intent.putExtra("some_key", points);
startActivity(intent);
}
在我的另一个活动中:
Bundle bundle = getIntent().getExtras();
int point = bundle.getInt("some_key");
当我想在那里显示我的分数时,我会按
进行TextView scoreTextView = (TextView) findViewById(R.id.score);
scoreTextView.setText("" + point);
当分数<= 10时,它可以正常工作,但是当它高于最后一个数字时显示..
还有我的分数TextView
:
对不起,这是我的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="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:text="99"
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="/40"
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>
这是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>
我有我的RadioButton,当有人点击其中一个时,如果他们正确回答,他们会获得+2,如果他们没有,则获得-1点。问题是当有人正确回答大多数问题时,以及当得分为10+时。如果我有16分,则显示6而不是16分。
这是应用程序中的screeshots,我回答所有问题,我的分数是16,但它显示6 Picture 1 Picture 2
quiz_activity我增加或减少我的分数:
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("");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
}
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);
}
}
答案 0 :(得分:0)
将以下LinearLayout
&#39; layout_width
更改为android:layout_width="match_parent"
。
然后在同一个LinearLayout
中添加android:gravity="center"
。这会使TextView
中的LinearLayout
居中。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal">
<TextView
android:text="99"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/score"
android:textSize="25sp"
android:textColor="@color/colorAccent"/>
<TextView
android:text="/40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/score1"
android:textSize="25sp"
android:gravity="center"/>
</LinearLayout>