布局方法:
Linearlayout线性布局:
android:orientation="vertical"则每个控件占据一行
android:orientation="horizontal"则每个控件占据一列
长度单位:
px---像素
Dip--(dots per inch)
Dp--(device independent piexls)通常用来针对控件的大小
换算公式:px=dp*(dpi/160)
在dpi为160的屏幕上:1dp=1px
Sp:scaled pixels,通常用来指定字体的大小
控件之------多选按钮:
实现代码:
package org.jason;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
importandroid.view.View;
importandroid.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
publicclass MainActivity extends Activity {
private CheckBox runB;
private CheckBox swimB;
private CheckBox sleepB;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
setContentView(R.layout.liner_layout);
//通过findViewById方法生成控件对象
runB=(CheckBox)findViewById(R.id.runid);
swimB=(CheckBox)findViewById(R.id.swimid);
sleepB=(CheckBox)findViewById(R.id.sleepid);
/*OnBoxClickListener listener=new OnBoxClickListener();
runB.setOnClickListener(listener);
sleepB.setOnClickListener(listener);
swimB.setOnClickListener(listener);*/
//生成listener对象
OnCheckedListener listener=new OnCheckedListener();
//调用对象的setOnCheckedChangeListener方法绑定监听器
runB.setOnCheckedChangeListener(listener);
sleepB.setOnCheckedChangeListener(listener);
swimB.setOnCheckedChangeListener(listener);
}
//声明一个类,实现OnCheckedChangeListener接口
class OnCheckedListener implements OnCheckedChangeListener{
@Override
publicvoid onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
//getId()方法通过控件对象获取Id
//如果全选按钮,则使用setCheck(boolean checked)方法
if(buttonView.getId()==R.id.runid){
System.out.println("run");
}elseif(buttonView.getId()==R.id.swimid){
System.out.println("swim");
}elseif(buttonView.getId()==R.id.sleepid){
System.out.println("sleep");
}
//isChecked返回值boolean类型,判断是否选中
if(isChecked){
System.out.println("checked");
}else{
System.out.println("cancel");
}
}
}
/*class OnBoxClickListener implements OnClickListener{
@Override
public void onClick(View view) {
CheckBox box=(CheckBox)view;
if(view.getId()==R.id.runid){
System.out.println("run");
}else if(view.getId()==R.id.sleepid){
System.out.println("sleep");
}else if(view.getId()==R.id.swimid){
System.out.println("swim");
}
if(box.isChecked()){
System.out.println("box is checked");
}else{
System.out.println("box is unchecked");
}
}
单选按钮(RadioButton)用法:
<RadioGroup>
<RadioButton
Andriod:…/>
<RadioButton
Android…./>
</RadioGroup>
Activity.java的方法与多选按钮类似
Radiobutton与checkbox都使用同一个onCheckedChangedlistener接口,而radioGroup实现的是Radio接口