按下按钮启动游戏循环

时间:2017-05-23 15:30:38

标签: java android android-fragments android-activity android-button

我创建了一个活动PlayActivity,用于启动新线程和游戏循环。当我将这个类作为我的启动器活动(MainActivity)时,它可以完美地运行。但我显然不希望我的应用程序从游戏循环开始!在我的MainActivity中,我有不同的标签作为片段,在我的主页选项卡中,我有一个按钮。我想要点击按钮开始游戏。这是我的PlayActivity:

public class PLayActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        Constants.SCREEN_WIDTH = dm.widthPixels;
        Constants.SCREEN_HEIGHT = dm.heightPixels;

        try {
            InputStream is = getAssets().open("test 3.xml");
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            setContentView(new PlayPanel(this, is));
        } catch(IOException e) {e.printStackTrace();}
    }
}

主要活动:

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

        View inflatedView = getLayoutInflater().inflate(R.layout.home, null);
        basic = (Button) inflatedView.findViewById(R.id.basic);
        intermediate = (Button) inflatedView.findViewById((R.id.intermidiate));

        basic.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg) {

                // Start NewActivity.class
                Intent basicIntent = new Intent(MainActivity.this, PLayActivity.class);
                startActivity(basicIntent);
            }
        });

Home.java:

public class Home extends Fragment {

    Button basic;
    Button intermediate;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        final View view = inflater.inflate(R.layout.home, container, false);
        return view;
    }
}

和home.xml

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

    <Button
        android:text="Basic"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:layout_marginTop="0dp"
        android:id="@+id/basic"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:text="Intermidiate"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:layout_marginTop="0dp"
        android:id="@+id/intermidiate"
        android:layout_below="@+id/basic"
        android:layout_centerHorizontal="true" />

    <Button
        android:text="Advanced"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:layout_marginTop="0dp"
        android:id="@+id/advanced"
        android:layout_below="@+id/intermidiate"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

当我点击按钮时没有任何反应!我该怎么办?

0 个答案:

没有答案