我剛剛提出了一個要求添加問題的應用程序。我想刷新整個應用程序,我該怎麼做?我只需要知道如何刷新它 - 這些問題,我可以自己找出來的。如何在應用程序(活動)仍在運行時刷新?
什麼我的目標僅僅是在屏幕返回到開始屏幕,而不是應用程序自行關閉,然後重新啓動(如果這是什麼,這是可用的唯一途徑)
的java:
package com.example.android.mathapp;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.AnimationDrawable;
import android.media.Image;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity{
private boolean correct;
private String questionTxt, option1String, option2String, option3String, option4String;
private int answer;
TextView option1Text, option2Text, option3Text, option4Text;
private String[] ansString = new String[4];
private int counter = 0;
float f = 100;
public void correctAnswer()
{
int first = (int)(Math.random() * 10);
int second = (int)(Math.random() * 10);
answer = first + second;
questionTxt = first + " + " + second + " = ?";
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
/*ImageView rocketImage = (ImageView) findViewById(R.id.img_animation);
rocketImage.setBackgroundResource(R.drawable.greencartooncar);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();*/
TextView questionText = (TextView)findViewById(R.id.question);
correctAnswer();
questionText.setText(questionTxt + "");
TextView directionsEnd = (TextView)findViewById(R.id.directions);
directionsEnd.setText("Get 10 questions right to finish the race!");
makeOptions(answer);
option1Text = (TextView)findViewById(R.id.option1);
option1Text.setText(ansString[0]);
option2Text = (TextView)findViewById(R.id.option2);
option2Text.setText(ansString[1]);
option3Text = (TextView)findViewById(R.id.option3);
option3Text.setText(ansString[2]);
option4Text = (TextView)findViewById(R.id.option4);
option4Text.setText(ansString[3]);
setString();
//noSameAnswer();
}
private String[] makeOptions(int correctAns) {
for(int i=0;i<ansString.length;i++)
{
ansString[i] = null;
}
int randomPos = (new Random().nextInt(4 - 1) + 1);
ansString[randomPos]=correctAns + "";
for (int i=0;i<4;i++){
int randomAns = (int)(Math.random() * 10 + Math.random() * 10);
if(ansString[i] == null){
ansString[i]=randomAns + "";
}
}
if (ansString[0].equals(ansString[1])||ansString[0].equals(ansString[2])||ansString[0].equals(ansString[3])
||ansString[1].equals(ansString[2]) ||ansString[1].equals(ansString[3])||ansString[2].equals(ansString[3]))
{
makeOptions(answer);
}
return ansString;
}
/*public void noSameAnswer()
{
if (option1String.equals(option2String)||option1String.equals(option3String)||option1String.equals(option4String)
||option2String.equals(option3String)||option2String.equals(option4String)||option3String.equals(option4String))
{
makeOptions(answer);
option1Text = (TextView)findViewById(R.id.option1);
option1Text.setText(ansString[0]);
option2Text = (TextView)findViewById(R.id.option2);
option2Text.setText(ansString[1]);
option3Text = (TextView)findViewById(R.id.option3);
option3Text.setText(ansString[2]);
option4Text = (TextView)findViewById(R.id.option4);
option4Text.setText(ansString[3]);
setString();
noSameAnswer();
}
}*/
public void onClick3(View v) {
if (option3String.equals(Integer.toString(answer))) {
counter++;
TextView questionText = (TextView) findViewById(R.id.question);
correctAnswer();
questionText.setText(questionTxt + "");
makeOptions(answer);
option1Text = (TextView)findViewById(R.id.option1);
option1Text.setText(ansString[0]);
option2Text = (TextView)findViewById(R.id.option2);
option2Text.setText(ansString[1]);
option3Text = (TextView)findViewById(R.id.option3);
option3Text.setText(ansString[2]);
option4Text = (TextView)findViewById(R.id.option4);
option4Text.setText(ansString[3]);
setString();
animate();
//noSameAnswer();
}
}
public void onClick1(View v) {
if (option1String.equals(Integer.toString(answer))) {
counter++;
TextView questionText = (TextView) findViewById(R.id.question);
correctAnswer();
questionText.setText(questionTxt + "");
makeOptions(answer);
option1Text = (TextView)findViewById(R.id.option1);
option1Text.setText(ansString[0]);
option2Text = (TextView)findViewById(R.id.option2);
option2Text.setText(ansString[1]);
option3Text = (TextView)findViewById(R.id.option3);
option3Text.setText(ansString[2]);
option4Text = (TextView)findViewById(R.id.option4);
option4Text.setText(ansString[3]);
setString();
animate();
//noSameAnswer();
}
}
public void onClick2(View v) {
if (option2String.equals(Integer.toString(answer))) {
counter++;
TextView questionText = (TextView) findViewById(R.id.question);
correctAnswer();
questionText.setText(questionTxt + "");
makeOptions(answer);
option1Text = (TextView)findViewById(R.id.option1);
option1Text.setText(ansString[0]);
option2Text = (TextView)findViewById(R.id.option2);
option2Text.setText(ansString[1]);
option3Text = (TextView)findViewById(R.id.option3);
option3Text.setText(ansString[2]);
option4Text = (TextView)findViewById(R.id.option4);
option4Text.setText(ansString[3]);
setString();
animate();
//noSameAnswer();
}
}
public void onClick4(View v) {
if (option4String.equals(Integer.toString(answer))) {
counter++;
TextView questionText = (TextView) findViewById(R.id.question);
correctAnswer();
questionText.setText(questionTxt + "");
makeOptions(answer);
option1Text = (TextView)findViewById(R.id.option1);
option1Text.setText(ansString[0]);
option2Text = (TextView)findViewById(R.id.option2);
option2Text.setText(ansString[1]);
option3Text = (TextView)findViewById(R.id.option3);
option3Text.setText(ansString[2]);
option4Text = (TextView)findViewById(R.id.option4);
option4Text.setText(ansString[3]);
setString();
animate();
//noSameAnswer();
}
}
public void setString()
{
option1String = option1Text.getText().toString();
option2String = option2Text.getText().toString();
option3String = option3Text.getText().toString();
option4String = option4Text.getText().toString();
}
public void animate()
{
ImageView img_animation = (ImageView) findViewById(R.id.car);
TranslateAnimation animation = new TranslateAnimation(f - 100, f,
0.0f, 0.0f);
animation.setDuration(1000);
animation.setFillAfter(true);
img_animation.startAnimation(animation);
f += 100;
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:screenOrientation="portrait"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bigboi"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android.mathapp.MainActivity">
- >
<TextView
android:id="@+id/option3"
android:layout_width="175dp"
android:layout_height="120dp"
android:background = "@drawable/rounded_corner"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:textAppearance="?android:textAppearanceLarge"
android:gravity="center"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:screenOrientation="portrait"
android:onClick="onClick3"
android:clickable="true"
android:text="option 3" />
<TextView
android:id="@+id/option1"
android:text="option 1"
android:layout_width="175dp"
android:layout_height="120dp"
android:background = "@drawable/rounded_corner"
android:textAppearance="?android:textAppearanceLarge"
android:gravity="center"
android:screenOrientation="portrait"
android:layout_above="@+id/option3"
android:onClick="onClick1"
android:clickable="true"/>
<TextView
android:id="@+id/option2"
android:layout_width="175dp"
android:layout_height="120dp"
android:background = "@drawable/rounded_corner"
android:textAppearance="?android:textAppearanceLarge"
android:gravity="center"
android:text="option 2"
android:screenOrientation="portrait"
android:layout_above="@+id/option3"
android:layout_toRightOf="@+id/option3"
android:onClick="onClick2"
android:clickable="true"/>
<ImageView
android:id="@+id/car"
android:layout_width="60dp"
android:layout_height="30dp"
android:src="@drawable/greencartooncar"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/option4"
android:layout_width="175dp"
android:layout_height="120dp"
android:background = "@drawable/rounded_corner"
android:textAppearance="?android:textAppearanceLarge"
android:gravity="center"
android:text="option 4"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/option3"
android:layout_toEndOf="@+id/option3"
android:screenOrientation="portrait"
android:onClick="onClick4"
android:clickable="true"/>
<TextView
android:id="@+id/question"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:layout_above="@+id/option1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="11dp"
android:textSize="30sp"
android:screenOrientation="portrait"/>
<TextView
android:id="@+id/directions"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:layout_above="@id/question"
android:textSize="15sp"/>
<ImageView
android:id="@+id/finish"
android:layout_width="50dp"
android:layout_height="75dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:src="@drawable/finish"/>
<TextView
android:layout_width="20dp"
android:layout_height="10dp"
android:layout_above="@+id/directions"
android:layout_alignParentRight="true"
android:text="FINISH"
android:layout_below="@+id/finish"
android:layout_alignLeft="@+id/finish"
android:layout_alignStart="@+id/finish" />
你的意思是刷新,對嗎?另外,添加你的代碼。 –
我們能否看到您的代碼,以便我們知道需要在「重新啓動」時重置哪些內容? –
@Nilesh Singh沒有作者批准,但審稿人,我認爲你是通過添加你自己的話來編輯它並獲得批准,因爲答案可能會有所不同取決於這些words.For他想刷新然後他不想**回到開始屏幕** –