這是佈局xml文件:java
<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" > <TextView android:id="@+id/hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:id="@+id/input" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:drawable/editbox_background" android:layout_below="@id/hello" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/text" android:layout_below="@id/input" /> <RadioGroup android:id="@+id/radio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/text" > <RadioButton android:id="@+id/man" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/man"/> <RadioButton android:id="@+id/gril" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/gril" /> </RadioGroup> <TextView android:id="@+id/choose" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/choose" android:layout_below="@id/radio" /> <CheckBox android:id="@+id/one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/one" android:layout_below="@id/choose" /> <CheckBox android:id="@+id/two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/two" android:layout_below="@id/choose" android:layout_toRightOf="@id/one" /> <CheckBox android:id="@+id/three" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/three" android:layout_toRightOf="@id/two" android:layout_below="@id/choose" /> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ok" android:layout_below="@id/two" /> </RelativeLayout>
這是String配置文件android
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">layout2</string> <string name="action_settings">Settings</string> <string name="hello">請輸入您的名字:</string> <string name="text">請選擇您的性別:</string> <string name="man">男</string> <string name="gril">女</string> <string name="choose">請選擇你喜歡的水果:</string> <string name="one">芒果</string> <string name="two">香蕉</string> <string name="three">椰子</string> <string name="ok">提交</string> </resources>
最後是實現的JAVA:app
package com.example.layout2; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.Toast; public class MainActivity extends Activity { private EditText edit; private Button button; private RadioGroup group; private CheckBox box1; private CheckBox box2; private CheckBox box3; private BoxChange change; private String str=""; private String sex=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); edit=(EditText) findViewById(R.id.input); button=(Button) findViewById(R.id.ok); group=(RadioGroup) findViewById(R.id.radio); box1=(CheckBox) findViewById(R.id.one); box2=(CheckBox) findViewById(R.id.two); box3=(CheckBox) findViewById(R.id.three); group.setOnCheckedChangeListener(new RadioChange()); change=new BoxChange(); box1.setOnCheckedChangeListener(change); box2.setOnCheckedChangeListener(change); box3.setOnCheckedChangeListener(change); button.setOnClickListener(new ButtonClick()); } @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; } class RadioChange implements OnCheckedChangeListener{ public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radio=(RadioButton) group.findViewById(checkedId); sex=radio.getText().toString(); } } class BoxChange implements android.widget.CompoundButton.OnCheckedChangeListener{ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { CheckBox box=(CheckBox) buttonView; String boxStr=box.getText().toString()+"、"; if(isChecked){ str +=boxStr; }else{ System.out.println(boxStr); str=str.replace(boxStr, ""); } } } class ButtonClick implements OnClickListener{ public void onClick(View v) { String name=edit.getText().toString(); Toast.makeText(MainActivity.this, "親愛的:"+name+",您的性別是:"+sex+",您喜歡的水果有:"+str, Toast.LENGTH_SHORT).show(); } } }