Android的常用控件有,Android学习笔记-常用控件

单选按钮 Radio

0818b9ca8b590ca3270a3433284dd417.png

android:id="@+id/genderGroup"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical">

android:id="@+id/femaleButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/female"/>

android:id="@+id/maleButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/male"/>

genderGroup = (RadioGroup) findViewById(R.id.genderGroup);

maleButton = (RadioButton) findViewById(R.id.maleButton);

femaleButton = (RadioButton) findViewById(R.id.femaleButton);

//...

genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

// TODO Auto-generated method stub

if (femaleButton.getId() == checkedId) {

System.out.println("female");

Toast.makeText(MainActivity.this, "female",

Toast.LENGTH_SHORT).show();

} else if (maleButton.getId() == checkedId) {

System.out.println("female");

Toast.makeText(MainActivity.this, "male",

Toast.LENGTH_SHORT).show();

}

}

});

多选 CheckBox

0818b9ca8b590ca3270a3433284dd417.png

android:id="@+id/swim"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/genderGroup"

android:text="@string/swim"/>

android:id="@+id/read"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/swim"

android:text="@string/read"/>

android:id="@+id/run"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/read"

android:text="@string/run"/>

swimBox = (CheckBox) findViewById(R.id.swim);

runBox = (CheckBox) findViewById(R.id.run);

readBox = (CheckBox) findViewById(R.id.read);

//...

swimBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView,

boolean isChecked) {

// TODO Auto-generated method stub

if (isChecked) {

System.out.println("Swim is checked");

} else {

System.out.println("Swim is unchecked");

}

}

});

readBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView,

boolean isChecked) {

// TODO Auto-generated method stub

if (isChecked) {

System.out.println("Read is checked");

} else {

System.out.println("Read is unchecked");

}

}

});

runBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView,

boolean isChecked) {

// TODO Auto-generated method stub

if (isChecked) {

System.out.println("Run is checked");

} else {

System.out.println("Run is unchecked");

}

}

});

}

进度条 ProgressBar

0818b9ca8b590ca3270a3433284dd417.png

android:id="@+id/firstBar"

style="?android:attr/progressBarStyleHorizontal"

android:layout_width="200dp"

android:layout_height="wrap_content"

android:visibility="gone"/>

android:id="@+id/secondBar"

style="?android:attr/progressBarStyle"

android:layout_width="200dp"

android:layout_height="wrap_content"

android:layout_below="@id/firstBar"

android:visibility="gone"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/secondBar"

android:text="开始"/>

public class MainActivity extends ActionBarActivity {

private ProgressBar firstBar = null;

private ProgressBar secondBar = null;

private Button myButon = null;

private int i = 0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

firstBar = (ProgressBar) findViewById(R.id.firstBar);

secondBar = (ProgressBar) findViewById(R.id.secondBar);

myButon = (Button) findViewById(R.id.myButton);

myButon.setOnClickListener(new ButtonListener());

}

class ButtonListener implements OnClickListener{

@Override

public void onClick(View v) {

if (i == 0) {

firstBar.setVisibility(View.VISIBLE);

secondBar.setVisibility(View.VISIBLE);

}else if (i 

//设置朱进度条的值

firstBar.setProgress(i);

//设置第二进度条的值

secondBar.setSecondaryProgress(i + 10);

//默认的进度条无法显示进行的状态

//secondBar.setProgress(i);

}else {

firstBar.setVisibility(View.GONE);

secondBar.setVisibility(View.GONE);

}

i = i + 10;

}

}

}

列表 ListView

0818b9ca8b590ca3270a3433284dd417.png

main.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/ListLinearLayout"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:drawSelectorOnTop="false"

android:scrollbars="vertical"/>

user.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal" >

android:id="@+id/user_name"

android:layout_width="180dip"

android:layout_height="30dip"

android:textSize="10pt"

android:singleLine="true"/>

android:id="@+id/user_ip"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:textSize="10pt"

android:gravity="right"/>

MainActivity.java

public class MainActivity extends ListActivity{

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

ArrayList> list = new ArrayList>();

HashMap map1 = new HashMap();

HashMap map2 = new HashMap();

HashMap map3 = new HashMap();

map1.put("user_name", "admin1");

map1.put("user_ip", "192.168.24.214");

map2.put("user_name", "admin2");

map2.put("user_ip", "192.168.24.215");

map3.put("user_name", "admin3");

map3.put("user_ip", "192.168.24.216");

list.add(map1);

list.add(map2);

list.add(map3);

SimpleAdapter listAdapter = new SimpleAdapter(this, list, R.layout.user, new String[]{"user_name", "user_ip"}, new int[]{R.id.user_ip, R.id.user_name});

setListAdapter(listAdapter);

}

@Override

protected void onListItemClick(ListView l, View v, int position, long id) {

// TODO Auto-generated method stub

super.onListItemClick(l, v, position, id);

System.out.println("id:" + id);

System.out.println("position:" + position);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值