单击android中的按钮时出错

时间:2013-06-24 13:09:34

标签: android

我是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     import android.app.Activity;     import android.os.Bundle;     import android.view.View;     import android.view.View.OnClickListener;     import 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>

2 个答案:

答案 0 :(得分: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));
        }
    };
}

让我知道结果。

答案 1 :(得分:0)

  

不建议多次调用setContentView()。正常   在同一活动中布局之间切换的方法是使用a   ViewFlipper或FrameLayout(请参阅调用setContentView()多次   时间和我如何制作动态翻转屏幕(就像那样   iPhone)在Android)

     

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