請原諒我我是新的編程爲Android。我試圖將ImageButton鏈接到一個名爲crafting的類,然後在該類中啓動一個名爲crafting的xml文件。該按鈕似乎沒有在avd中做任何事情,但我正在努力查看我的錯誤在哪裏。試圖將一個圖像按鈕鏈接到一個類Android
下面是代碼:
在MainActivity:
package cf.angryman.app;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_test);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void SetupCraftingButton() {
ImageButton craftingButton = (ImageButton) findViewById(R.id.crafting);
craftingButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), Crafting.class));
}
});
}
}
的工藝加工類:
package cf.angryman.app;
import android.os.Bundle;
import android.app.Activity;
public class Crafting extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.crafting);
}
}
下面是XML代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/home_background" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"
android:layout_centerInParent="true" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="341dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/crafting"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:background="@null"
android:scaleType="fitXY"
android:src="@drawable/workbench" />
</LinearLayout>
</ScrollView>
</LinearLayout>
希望你能幫幫我!
感謝,
亞歷
你可以發佈你的按鈕的XML? – 2014-10-26 21:41:49
我用XML更新了原始帖子 – user3407957 2014-10-26 21:44:24