0

こんにちは、ボタンをクリックしても機能しない理由をお聞きしたいのですが、機能する他のレイアウトボタンコードをコピーして、自分で作成しようとしましたが、まだ機能しませんか?

ここに私のコードがあります

.java パッケージ com.thesis.logipic;

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 に答える