我刚刚制作了一个应用程序,询问另外一个问题。我想刷新整个应用程序,我该怎么做?我需要知道的是如何刷新它 - 问题,我可以自己弄清楚。
我的目标只是让屏幕返回到开始屏幕,而不是应用程序自行关闭然后重新启动。(如果这是唯一的方法,那就是它可用)
的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">
- &GT;
<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" />
答案 0 :(得分:1)
您可以使用此代码
Intent mStartActivity = new Intent(YourCurrActivity.this, ClassYouNeedToReLaunch.class);
int mPendingIntentId = 123456;
PendingIntent mPendingIntent = PendingIntent.getActivity(YourCurrActivity.this, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager)YourCurrActivity.this.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
如果要在不同的活动中调用它,请创建静态方法
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
public class Util {
static void reStartActivity(Context currant, Class needToLoad ){
Intent mStartActivity = new Intent(currant,needToLoad);
int mPendingIntentId = 123456;
PendingIntent mPendingIntent = PendingIntent.getActivity(currant, mPendingIntentId,mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager)currant.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
}
}
并在任何课程Util.reStartActivity(YourCurrActivity.this,ClassYouNeedToReLaunch.class);
可能是你的问题。
或
如果您有splashActivity
或某些similler,请使用普通Intent
再次调用该活动。