这个阶段在学习android的相关基本UI现将相关练习的代码粘贴在此便于后期学习之用(radio控件)
效果图:
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sex" /> <RadioGroup
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/boy" /> <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/girl" />
</RadioGroup> <Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btnSel" /> </LinearLayout>
MainActivity.java
package com.skycc.radio; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
/**
* * @Description: 单选按钮练习 * @author:skycc * @time:2014-4-10 下午3:53:15
*/
public class MainActivity extends Activity {
private Button button;
private RadioGroup group; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
button = (Button) this.findViewById(R.id.btn);
group = (RadioGroup) this.findViewById(R.id.sex);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int len = group.getChildCount();//获取这个group中的radio数
String msgString = "";
for (int i = 0; i < len; i++) {
RadioButton radioButton = (RadioButton) group.getChildAt(i);//将每个group中的radio进行遍历,并判断是否被选中
if (radioButton.isChecked()) { msgString = "选中了性别" + radioButton.getText().toString();
break;
}
}
Toast.makeText(MainActivity.this, msgString, 1).show();
}
});
}
}
hi,完成运行