我是 android 应用程序开发的新手。我写了一些代码,checkbox
但我没有得到预期的结果。所有的checkboxes
都显示在同一行。如何checkbox
在单独的行上显示每个?
activity_main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<CheckBox
android:id="@+id/chechbox_bike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Bike"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="@+id/chechbox_car"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Car"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="@+id/chechbox_office"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Office"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="@+id/chechbox_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Home"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="@+id/chechbox_locker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Locker"
android:onClick="onCheckboxClicked"/>
MainActivity.java
package com.example.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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 onCheckboxClicked(View view){
boolean checked=((CheckBox) view).isChecked();
switch (view.getId()){
case R.id.chechbox_bike:
if (checked)
System.out.println("bike");
case R.id.chechbox_car:
if (checked)
System.out.println("Car");
case R.id.chechbox_home:
if (checked)
System.out.println("Home");
case R.id.chechbox_locker:
if (checked)
System.out.println("Locker");
case R.id.chechbox_office:
if (checked)
System.out.println("Office");
}
}
}
谢谢你