-4

我是 android 新手,当我点击按钮时,什么也没有发生。所以请帮助摆脱这个问题。提前致谢。

MainActivity.java

import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        Button btn = (Button)findViewById(R.id.hom_sel_enter_btn);
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                setContentView(R.layout.menu_selection_layout);
            }
        });
    }
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" android:background="@drawable/loadingscreen">
        <RelativeLayout>
            android:id="@+id/InnerRelativeLayout"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" >
            <Button>
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/hom_sel_enter_btn"
                android:layout_gravity="center_horizontal"
                android:soundEffectsEnabled="true"
                android:background="@drawable/ent_sel_bn" >
            </Button>
        </RelativeLayout>
    </RelativeLayout>


app_menu_sel.java 导入 android.app.Activity;导入android.os.Bundle;导入android.view.View;导入 android.view.View.OnClickListener;导入android.widget.Button;

public class app_menu_sel extends Activity{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_selection_layout);
        Button btn1 = (Button)findViewById(R.id.hom_but_selection_top);
        btn1.setOnClickListener(btnListener);
    }

    private OnClickListener btnListener = new OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.activity_main);
        }
    };
}


menu_selection_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout>
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <Button>
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/hom_but_selection_top"
            android:clickable="true"
            android:background="@drawable/home">
        </Button>
    </LinearLayout>
</LinearLayout>


显现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.display"
    android:versionCode="1"
    android:versionName="1.0" >

    <application>
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity>
            android:name="com.example.display.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation = "portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name="com.example.display.app_menu_sel"
            android:label="@string/app_menu_sel"
        android:screenOrientation = "portrait">
        </activity>
    </application>
</manifest>
4

2 回答 2

4

替换代码

public class MainActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        Button btn = (Button) findViewById(R.id.hom_sel_enter_btn);
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), app_menu_sel.class));
            }
        });
    }
}

public class app_menu_sel extends Activity{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_selection_layout);
        Button btn1 = (Button)findViewById(R.id.hom_but_selection_top);
        btn1.setOnClickListener(btnListener);
    }

    private OnClickListener btnListener = new OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), MainActivity.class));
        }
    };
}

让我知道结果。

于 2013-06-24T14:07:08.247 回答
0

不建议多次调用 setContentView()。在同一活动中切换布局的正常方法是使用 ViewFlipper 或 FrameLayout(请参阅多次调用 setContentView() 以及如何在 Android 中制作动态翻转屏幕(如 iPhone 的屏幕))

或者,您可以使用 ViewSwitcher,它似乎非常适合您的需求。在http://inphamousdevelopment.wordpress.com/2010/10/11/using-a-viewswitcher-in-your-android-xml-layouts/上有一个很好的详细教程

于 2013-06-24T13:59:39.473 回答