0

嗨,我想问一下为什么我的按钮在单击时不起作用我只是复制了其他有效的布局按钮代码并尝试自己创建它但仍然不起作用?

这是我的代码

.java 包 com.thesis.logicic;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;


public class Menu extends Activity 
{

Button beginner;

@Override
protected void onCreate(Bundle MenuButtons) {
    super.onCreate(MenuButtons);
}

public void onClick(View v) 
{
    Button clickedButton = (Button) v;
    switch (clickedButton.getId()) 
    {

    case R.id.btnBeginner:
        setContentView(R.layout.gameplay);
        break;

    case R.id.btnLearner:
        setContentView(R.layout.menu);
        break;
    }
 }

}

.xml

<ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/categories" >

<Button
    android:id="@+id/btnLearner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btnBeginner"
    android:layout_centerHorizontal="true"
    android:background="@drawable/learner_menu" />

<Button
    android:id="@+id/btnBeginner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btnLearner"
    android:layout_alignParentTop="true"
    android:background="@drawable/beginner_menu" />

</RelativeLayout>

</ScrollView>

</LinearLayout>

和清单

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".Splash"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".ThesisActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.thesis.logipic.THESISACTIVITY" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity
        android:name=".logipic.Menu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.thesis.logipic.MENU" />
            <category android:name="android.intent.category.ALTERNATIVE" />
        </intent-filter>
    </activity>

    <activity
        android:name=".Gameplay"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.thesis.logipic.GAMEPLAY" />
            <category android:name="android.intent.category.ALTERNATIVE" />
        </intent-filter>
    </activity>

    <activity
        android:name=".Beginner"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.thesis.logipic.BEGINNER" />
            <category android:name="android.intent.category.ALTERNATIVE" />
        </intent-filter>
    </activity>

</application>

</manifest>
4

3 回答 3

0

将 contentview 设置为您的 onCreate 方法。

 setContentView(R.layout.menu);

然后将以下标记添加到 xml Button 元素中。

 android:onClick="onClick"

编辑

为了您的方便。它是您修改后的代码。

  @Override
   protected void onCreate(Bundle MenuButtons) {
   super.onCreate(MenuButtons);
   setContentView(R.layout.menu); 
  }

和xml

<ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/categories" >

<Button
    android:id="@+id/btnLearner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btnBeginner"
    android:layout_centerHorizontal="true"
    android:background="@drawable/learner_menu"
    android:onClick="onClick" 
    />

<Button
    android:id="@+id/btnBeginner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btnLearner"
    android:layout_alignParentTop="true"
    android:background="@drawable/beginner_menu"   
    android:onClick="onClick"
    />


</RelativeLayout>

</ScrollView>
于 2013-10-05T20:24:25.367 回答
0

尝试这个:

@Override
protected void onCreate(Bundle MenuButtons) {
    super(MenuButtons);
    ((Button) findViewById(R.id.btnBeginner)).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            setContentView(R.layout.gameplay);
        }
    });
    ((Button) findViewById(R.id.btnLearner)).setOnClickListener(new OnClickListener() {
        @Override
         public void onClick(View v) {
             setContentView(R.layout.menu);
         }
    });
}
于 2013-10-05T20:16:09.427 回答
0

您必须修改您的活动:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Menu extends Activity implements OnClickListener {

    private Button beginner, learner;

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

        beginner = (Button) findViewById(R.id.btnBeginner);
        beginner.setOnClickListener(this);

        learner = (Button) findViewById(R.id.btnLearner);
        learner.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

        case R.id.btnBeginner:
            setContentView(R.layout.gameplay);
            break;

        case R.id.btnLearner:
            setContentView(R.layout.menu);
            break;
        }
    }

}
于 2013-10-05T20:22:33.207 回答