您好我想问为什么我的按钮不能正常工作点击我只是复制了我的其他布局按钮代码,并尝试自己创建但仍然不起作用?
这是我的代码
的.java package 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>
答案 0 :(得分: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);
}
});
}
答案 1 :(得分: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;
}
}
}
答案 2 :(得分: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>