xml(activity)应在关闭时在后台保持打开状态

时间:2014-03-20 15:23:45

标签: java android xml eclipse

我正在使用Eclipse创建一个测验Android应用程序,我做了它,以便每个问题都有自己的活动,现在一切正常,如果我回答qustions缓慢,但如果我做得更快,xml在后台保持打开我甚至添加onPause方法,但它仍然是开放的。我不知道如何使用线程,但有人告诉我它会使应用程序更快,所以xml将关闭。我希望有一个简单的解决我的问题,如果没有,任何人都可以解释我如何使用线程。

以下是我的一个xml布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/qh4"
    tools:context=".POV6" >

        <TextView
        android:id="@+id/povrat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="150dp"
        android:layout_marginTop="50dp"
        android:paddingLeft="@dimen/activity_vertical_margin"
        android:paddingRight="@dimen/activity_vertical_margin"
        android:text="The highest peak in North America is ?"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold"
        tools:ignore="HardcodedText" />

    <Button
        android:id="@+id/button2"
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/povrat"
        android:layout_centerHorizontal="true"
        android:onClick="tocan"
        android:text="Mount Mckinley"
        tools:ignore="HardcodedText" />

    <Button
        android:id="@+id/button3"
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_below="@+id/button2"
        android:onClick="netocanodgovor"
        android:text="Mount Everest"
        tools:ignore="HardcodedText" />

    <Button
        android:id="@+id/button4"
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button3"
        android:layout_below="@+id/button3"
        android:onClick="netocanodgovor"
        android:text="Mount Logan"
        tools:ignore="HardcodedText" />

    <Button
        android:id="@+id/button5"
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button4"
        android:layout_below="@+id/button4"
        android:onClick="netocanodgovor"
        android:text="Mount Rainier"
        tools:ignore="HardcodedText" />

    <SlidingDrawer
        android:id="@+id/slidingDrawer1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:content="@+id/content"
        android:handle="@+id/handle" >

        <Button
            android:id="@+id/handle"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Pull up to use Jokers !"
            tools:ignore="HardcodedText" />

        <LinearLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:baselineAligned="false" >

            <TabHost
                android:id="@+id/tabhost"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >

                    <TabWidget
                        android:id="@android:id/tabs"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" >
                    </TabWidget>

                    <FrameLayout
                        android:id="@android:id/tabcontent"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >

                        <RelativeLayout
                            android:id="@+id/tab1"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="#000000"
                            android:orientation="vertical" >

                            <TextView
                                android:id="@+id/rekord"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_centerHorizontal="true"
                                android:layout_marginLeft="10dp"
                                android:layout_marginRight="10dp"
                                android:layout_marginTop="50dp"
                                android:paddingBottom="20dp"
                                android:text="This joker remowes one wrong answer !"
                                android:textAppearance="?android:attr/textAppearanceMedium"
                                android:textColor="#FFFFFF"
                                android:textSize="20sp"
                                android:textStyle="bold"
                                tools:ignore="HardcodedText" />

                            <TextView
                                android:id="@+id/joker1text"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentLeft="true"
                                android:layout_below="@id/rekord"
                                android:layout_marginLeft="10dp"
                                android:text="Medium Text"
                                android:textAppearance="?android:attr/textAppearanceMedium"
                                android:textColor="#FFFFFF"
                                android:textSize="20sp"
                                android:textStyle="bold"
                                tools:ignore="HardcodedText" />

                            <Button
                                android:id="@+id/button6"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:layout_alignParentBottom="true"
                                android:layout_alignParentRight="true"
                                android:onClick="joker1"
                                android:text="Use this joker !"
                                tools:ignore="HardcodedText" />

                        </RelativeLayout>

                        <RelativeLayout
                            android:id="@+id/tab2"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="#000000"
                            android:orientation="vertical" >

                            <TextView
                                android:id="@+id/textView3"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_centerHorizontal="true"
                                android:layout_marginLeft="10dp"
                                android:layout_marginRight="10dp"
                                android:layout_marginTop="50dp"
                                android:paddingBottom="20dp"
                                android:text="This joker will sometimes give you the right answer (30% of all cases) ! "
                                android:textAppearance="?android:attr/textAppearanceMedium"
                                android:textColor="#FFFFFF"
                                android:textSize="20sp"
                                android:textStyle="bold"
                                tools:ignore="HardcodedText" />

                            <TextView
                                android:id="@+id/joker2text"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentLeft="true"
                                android:layout_below="@id/textView3"
                                android:layout_marginLeft="10dp"
                                android:text="Medium Text"
                                android:textAppearance="?android:attr/textAppearanceMedium"
                                android:textColor="#FFFFFF"
                                android:textSize="20sp"
                                android:textStyle="bold"
                                tools:ignore="HardcodedText" />

                            <TextView
                                android:id="@+id/joker2odgovor"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_below="@id/joker2text"
                                android:layout_marginLeft="10dp"
                                android:layout_marginRight="10dp"
                                android:layout_marginTop="50dp"
                                android:text="I think the right answer is Mount Mckinley "
                                android:textAppearance="?android:attr/textAppearanceMedium"
                                android:textStyle="bold"
                                android:visibility="invisible"
                                tools:ignore="HardcodedText" />

                            <Button
                                android:id="@+id/button8"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:layout_alignParentBottom="true"
                                android:layout_alignParentRight="true"
                                android:onClick="joker2"
                                android:text="Use this joker !"
                                tools:ignore="HardcodedText" />

                        </RelativeLayout>

                        <RelativeLayout
                            android:id="@+id/tab3"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="#000000"
                            android:orientation="vertical" >

                            <TextView
                                android:id="@+id/textView4"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_centerHorizontal="true"
                                android:layout_marginLeft="10dp"
                                android:layout_marginRight="10dp"
                                android:layout_marginTop="50dp"
                                android:paddingBottom="20dp"
                                android:text="This joker will skip this question. But you will get no points for it !"
                                android:textAppearance="?android:attr/textAppearanceMedium"
                                android:textColor="#FFFFFF"
                                android:textSize="20sp"
                                android:textStyle="bold"
                                tools:ignore="HardcodedText" />

                            <TextView
                                android:id="@+id/joker3text"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentLeft="true"
                                android:layout_below="@id/textView4"
                                android:layout_marginLeft="10dp"
                                android:text="Medium Text"
                                android:textAppearance="?android:attr/textAppearanceMedium"
                                android:textColor="#FFFFFF"
                                android:textSize="20sp"
                                android:textStyle="bold"
                                tools:ignore="HardcodedText" />

                            <Button
                                android:id="@+id/button10"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:layout_alignParentBottom="true"
                                android:layout_alignParentRight="true"
                                android:onClick="joker3"
                                android:text="Use this joker !"
                                tools:ignore="HardcodedText" />

                        </RelativeLayout>

                        <RelativeLayout
                            android:id="@+id/tab4"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="#000000"
                            android:orientation="vertical" >

                            <TextView
                                android:id="@+id/textView5"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_centerHorizontal="true"
                                android:layout_marginLeft="10dp"
                                android:layout_marginRight="10dp"
                                android:layout_marginTop="50dp"
                                android:paddingBottom="20dp"
                                android:text="This joker will give you the right answer !"
                                android:textAppearance="?android:attr/textAppearanceMedium"
                                android:textColor="#FFFFFF"
                                android:textSize="20sp"
                                android:textStyle="bold"
                                tools:ignore="HardcodedText" />

                            <TextView
                                android:id="@+id/joker4text"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentLeft="true"
                                android:layout_below="@id/textView5"
                                android:layout_marginLeft="10dp"
                                android:text="Medium Text"
                                android:textAppearance="?android:attr/textAppearanceMedium"
                                android:textColor="#FFFFFF"
                                android:textSize="20sp"
                                android:textStyle="bold"
                                tools:ignore="HardcodedText" />

                            <Button
                                android:id="@+id/button12"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:layout_alignParentBottom="true"
                                android:layout_alignParentRight="true"
                                android:onClick="joker4"
                                android:text="Use this joker !"
                                tools:ignore="HardcodedText" />

                        </RelativeLayout>
                    </FrameLayout>
                </LinearLayout>
            </TabHost>
        </LinearLayout>
    </SlidingDrawer>

</RelativeLayout>

这是我的java代码:

package com.peky.smartornot;

import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;

public class POV6 extends Activity {
    Sql ulaz = new Sql(this);
    Sqlrecords rekordi = new Sqlrecords(this);
    TextView joke4text;
    TextView joke3text;
    TextView joke2text;
    TextView joke1text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pov6);
        sve();
    }

    public void sve() {
        // TODO Auto-generated method stub
        ulaz.open();
        int joker1 = ulaz.procitaj(), joker2 = ulaz.procitaj2(), joker3 = ulaz
                .procitaj3(), joker4 = ulaz.procitaj4();
        ulaz.close();
        TabHost joker = (TabHost) findViewById(R.id.tabhost);
        joker.setup();
        TabSpec izgled = joker.newTabSpec("tag1");
        izgled.setContent(R.id.tab1);
        izgled.setIndicator("Joker 1");
        joker.addTab(izgled);
        izgled = joker.newTabSpec("tag2");
        izgled.setContent(R.id.tab2);
        izgled.setIndicator("Joker 2");
        joker.addTab(izgled);
        izgled = joker.newTabSpec("tag3");
        izgled.setContent(R.id.tab3);
        izgled.setIndicator("Joker 3");
        joker.addTab(izgled);
        izgled = joker.newTabSpec("tag4");
        izgled.setContent(R.id.tab4);
        izgled.setIndicator("Joker 4");
        joker.addTab(izgled);
        joke1text = (TextView) findViewById(R.id.joker1text);
        joke1text.setText("You have " + joker1 + " jokers !");
        joke2text = (TextView) findViewById(R.id.joker2text);
        joke2text.setText("You have " + joker2 + " jokers !");
        joke3text = (TextView) findViewById(R.id.joker3text);
        joke3text.setText("You have " + joker3 + " jokers !");
        joke4text = (TextView) findViewById(R.id.joker4text);
        joke4text.setText("You have " + joker4 + " jokers !");
    }

    public void joker1(View view) {
        Button netocan = (Button) findViewById(R.id.button5);
        Button netocan2 = (Button) findViewById(R.id.button4);
        Button netocan3 = (Button) findViewById(R.id.button3);
        ulaz.open();
        int joker1 = ulaz.procitaj(), joker2, joker3, joker4;
        ulaz.close();
        if (joker1 != 0) {
            if (netocan.getVisibility() == View.VISIBLE) {
                netocan.setVisibility(View.INVISIBLE);
                ulaz.open();
                joker1 = joker1 - 1;
                joker2 = ulaz.procitaj2();
                joker3 = ulaz.procitaj3();
                joker4 = ulaz.procitaj4();
                ulaz.spremijoker(joker1, joker2, joker3, joker4);
                ulaz.close();
                joke1text = (TextView) findViewById(R.id.joker1text);
                joke1text.setText("You have " + joker1 + " jokers !");
            } else if (netocan2.getVisibility() == View.VISIBLE) {
                netocan2.setVisibility(View.INVISIBLE);
                ulaz.open();
                joker1 = joker1 - 1;
                joker2 = ulaz.procitaj2();
                joker3 = ulaz.procitaj3();
                joker4 = ulaz.procitaj4();
                ulaz.spremijoker(joker1, joker2, joker3, joker4);
                ulaz.close();
                joke1text = (TextView) findViewById(R.id.joker1text);
                joke1text.setText("You have " + joker1 + " jokers !");
            } else if (netocan3.getVisibility() == View.VISIBLE) {
                netocan3.setVisibility(View.INVISIBLE);
                ulaz.open();
                joker1 = joker1 - 1;
                joker2 = ulaz.procitaj2();
                joker3 = ulaz.procitaj3();
                joker4 = ulaz.procitaj4();
                ulaz.spremijoker(joker1, joker2, joker3, joker4);
                ulaz.close();
                joke1text = (TextView) findViewById(R.id.joker1text);
                joke1text.setText("You have " + joker1 + " jokers !");
            } else {
                Toast imasodgovor = Toast.makeText(getApplicationContext(),
                        "You can not use more JOKERS1 on this question !",
                        Toast.LENGTH_SHORT);
                imasodgovor.show();
            }

        } else {
            Toast nemasjokera = Toast.makeText(getApplicationContext(),
                    "Not enought JOKERS1 !", Toast.LENGTH_SHORT);
            nemasjokera.show();
        }

    }

    public void joker4(View view) {
        ulaz.open();
        int joker1, joker2, joker3, joker4 = ulaz.procitaj4();
        ulaz.close();
        Button netocan = (Button) findViewById(R.id.button5);
        Button netocan2 = (Button) findViewById(R.id.button4);
        Button netocan3 = (Button) findViewById(R.id.button3);
        if (joker4 != 0) {
            if (netocan.getVisibility() == View.VISIBLE
                    || netocan2.getVisibility() == View.VISIBLE
                    || netocan3.getVisibility() == View.VISIBLE) {
                netocan.setVisibility(View.INVISIBLE);
                netocan2.setVisibility(View.INVISIBLE);
                netocan3.setVisibility(View.INVISIBLE);
                ulaz.open();
                joker1 = ulaz.procitaj();
                joker2 = ulaz.procitaj2();
                joker3 = ulaz.procitaj3();
                joker4 = joker4 - 1;
                ;
                ulaz.spremijoker(joker1, joker2, joker3, joker4);
                ulaz.close();
                joke4text = (TextView) findViewById(R.id.joker4text);
                joke4text.setText("You have " + joker4 + " jokers !");
            } else {
                Toast imasodgovor = Toast.makeText(getApplicationContext(),
                        "You can not use more JOKERS4 on this question !",
                        Toast.LENGTH_SHORT);
                imasodgovor.show();
            }
        } else {
            Toast jokertext = Toast.makeText(getApplicationContext(),
                    "Not enought JOKERS4", Toast.LENGTH_SHORT);
            jokertext.show();

        }
    }

    public void joker3(View view) {
        ulaz.open();
        int joker3 = ulaz.procitaj3(), joker1, joker2, joker4;
        ulaz.close();
        if (joker3 != 0) {
            ulaz.open();
            joker1 = ulaz.procitaj();
            joker2 = ulaz.procitaj2();
            joker4 = ulaz.procitaj4();
            joker3 = joker3 - 1;
            ulaz.spremijoker(joker1, joker2, joker3, joker4);
            ulaz.close();
            Random crazy = new Random();

            switch (crazy.nextInt(4)) {
            case 0:
                Intent pokreni = new Intent(this, POV3.class);
                startActivity(pokreni);
                finish();
                break;
            case 1:
                Intent pokreni1 = new Intent(this, POV1.class);
                startActivity(pokreni1);
                finish();
                break;
            case 2:
                Intent pokreni11 = new Intent(this, POV4.class);
                startActivity(pokreni11);
                finish();
                break;
            case 3:
                Intent pokreni111 = new Intent(this, POV5.class);
                startActivity(pokreni111);
                break;
            }
        }

    }

    public void joker2(View view) {
        TextView joker2odgovor = (TextView) findViewById(R.id.joker2odgovor);
        ulaz.open();
        int joker1, joker2 = ulaz.procitaj2(), joker3, joker4;
        ulaz.close();
        if (joker2 != 0 && joker2odgovor.getVisibility() == View.INVISIBLE) {
            ulaz.open();
            joker1 = ulaz.procitaj();
            joker2 = joker2 - 1;
            joker3 = ulaz.procitaj3();
            joker4 = ulaz.procitaj4();
            ulaz.spremijoker(joker1, joker2, joker3, joker4);
            ulaz.close();
            joker2odgovor.setVisibility(View.VISIBLE);
            joke2text = (TextView) findViewById(R.id.joker2text);
            joke2text.setText("You have " + joker2 + " jokers !");

        } else {
            Toast odgovor = Toast.makeText(getApplicationContext(),
                    "Not enought JOKERS2 or already used on this question !",
                    Toast.LENGTH_SHORT);
            odgovor.show();
        }

    }

    public void tocan(View view) {
        Toast josip = Toast.makeText(getApplicationContext(), "Right answer !",
                Toast.LENGTH_SHORT);
        josip.show();

        int rekord = 0, ukupno = 0;
        rekordi.open();
        ukupno = rekordi.procitajukupno();
        rekord = rekordi.procitaj() + 10;
        rekordi.spremi(rekord, ukupno);
        rekordi.close();
        Random crazy = new Random();

        switch (crazy.nextInt(4)) {
        case 0:
            Intent pokreni = new Intent(this, POV3.class);
            startActivity(pokreni);
            finish();
            break;
        case 1:
            Intent pokreni1 = new Intent(this, POV1.class);
            startActivity(pokreni1);
            finish();
            break;
        case 2:
            Intent pokreni11 = new Intent(this, POV4.class);
            startActivity(pokreni11);
            finish();
            break;
        case 3:
            Intent pokreni111 = new Intent(this, POV5.class);
            startActivity(pokreni111);
            break;
        }
    }

    public void netocanodgovor(View view) {
        Intent gotovo = new Intent(this, Records.class);
        startActivity(gotovo);
        finish();
        Toast josip = Toast.makeText(getApplicationContext(),
                "Incorrect answer !", Toast.LENGTH_SHORT);
        josip.show();

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }
}

如果您需要更多信息,甚至是整个应用程序,我都可以发送给您!

0 个答案:

没有答案