我是新來的android編程和一些奇怪的事情發生在我的應用程序後,我在我的佈局中移動按鈕後。 所以有一個按鈕(開始),當點擊時,啓動另一個活動。這是代碼。Button2正在做按鈕1應該做的工作
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startbutton = (Button) findViewById(R.id.button1);
startbutton.setOnClickListener(new View.OnClickListener() { //STARTBUTTON
@Override
public void onClick(View arg0) { //LO QUE HACE EL START BUTTON
Intent intent = new Intent(MainActivity.this, JugarActivity.class);
startActivity(intent);
}
});
}
}
我的問題是測試我的應用程序時,當我點擊開始按鈕沒有任何反應,但是當我點擊另一個按鈕我在我的佈局(將Button4)它給我的第二個活動。 這是我的activity_main.xml以防萬一需要。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:minWidth="200dp"
android:text="@string/Start"
android:textSize="30sp" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="200dp"
android:minHeight="50dp"
android:minWidth="200dp"
android:text="@string/Jugarporcontinente"
android:textSize="30sp" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="@string/Aprende"
android:textSize="30sp" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="@string/Puntajes"
android:textSize="30sp" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="@string/Opciones"
android:textSize="30sp" />
</LinearLayout>
我不明白如何點擊一個甚至沒有代碼的按鈕,從另一個按鈕啓動代碼。歡迎任何幫助。謝謝。
我剛剛編輯你的問題,添加一些更好的縮進,實際上匹配大括號。爲了保持你的代碼可讀 - 對我們和你來說 - 保持匹配的大括號,並打算記住。 – cincodenada
提供的代碼是按鈕1? – iWumbo
@iWumbo你的問題沒有任何意義 - 這段代碼在'MainActivity :: onCreate'中,'startbutton'被'findViewById(R.id.button1)'引用,所以是的。 – cincodenada