本文导读: 一共介绍了四个控件: 1.RadioGroup,RadioButton 单选框 2.Toast 3.CheckBox 多选框 4.进度条 5.ListView |
<RadioGroup
android:id="@+id/sex"
android:layout_below="@id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
/>
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FeMale"
/>
</RadioGroup>
sexRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.male)
{
Toast.makeText(ClientMainActivity.this, "male", Toast.LENGTH_SHORT).show();
}
else if(checkedId == R.id.female)
{
Toast.makeText(ClientMainActivity.this, "female", Toast.LENGTH_SHORT).show();
}
}
});
Toast.makeText(ClientMainActivity.this, "female", Toast.LENGTH_SHORT).show();
如何改变Toast的显示位置?
Toast toast= Toast.makeText(getApplicationContext(), "Toast Here", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
toast.show();
<CheckBox
android:layout_below="@id/sex"
android:id="@+id/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Swim"
/>
<CheckBox
android:layout_below="@id/swim"
android:id="@+id/jump"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Jump"
/>
- 单选框和多选框的区别在于,多选框是给各个选项设置监听器
jumpCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
Toast.makeText(ClientMainActivity.this, "jump", Toast.LENGTH_SHORT).show();
}
}
});
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:visibility="gone"
/>
<ProgressBar
style="?android:attr/progressBarStyle"
...
/>
firstProgressBar = (ProgressBar) findViewById(R.id.progress1);
firstProgressBar.setVisibility(View.VISIBLE);
firstProgressBar.setVisibility(View.GONE);
firstProgressBar.setProgress(50);
firstProgressBar.setSecondaryProgress(70);
listview的使用一共分为3个部分:
第一个部分,编写lisview布局文件
第二个部分,编写每个条目的布局文件,例如user.xml
第三个部分,设置adapter
记得新建的activity要在mainfest中注册一下
listview的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/android:list">
</ListView>
</LinearLayout>
user的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/user_ip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
HashMap<String, String> map3 = new HashMap<String, String>();
map1.put("user name", "wang");
map1.put("user ip", "192.168.1.1");
map2.put("user name", "wu");
map2.put("user ip", "192.168.1.2");
map3.put("user name", "li");
map3.put("user ip", "192.168.1.3");
list.add(map1);
list.add(map2);
list.add(map3);
SimpleAdapter adapter = new SimpleAdapter(this,
list,
R.layout.user,
new String[]{"user name", "user ip"},
new int[]{R.id.user_name, R.id.user_ip});
setListAdapter(adapter);