Android 视图设计用户界面
基本视图
ToggleButton
,RadioButton
,RadioGroup
,AutoCompleteTextView
ToggleButton
用一个灯光指示器来显示选中/未选中状态1
2
3
4
5<ToggleButton
android:id="@+id/toggle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/rdbGp1"/>RadioButton
和RaidoGroup
RadioButton
有两个状态:选中或未选中。RaidoGroup
用来把一个或多个RaidoButton
视图组合在一起,从而只允许一个RaidoButton
被选中。视图效果如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<RadioGroup
android:id="@+id/rdbGp1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/star">
<RadioButton
android:id="@+id/rdb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton
android:id="@+id/rdb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
</RadioGroup>
AutoCompleteTextView
:与EditText
类似,输入时提供候选项1
2
3
4
5
6<AutoCompleteTextView
android:id="@+id/txtCountries"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView" />1
2
3
4ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,presidents);//创建容器
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtCountries);
textView.setThreshold(3);
textView.setAdapter(adapter);//设置候选项视图效果:
CheckBox
,RadioButton
视图事件CheckBox
:需将onClick()
方法的参数类型转换为一个CheckBox
,然后检查isCheckBox()
方法来确定其是否被选中1
2
3
4
5
6
7
8
9
10CheckBox checkBox = (CheckBox)findViewById(R.id.chkAutosave);
checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (((CheckBox) v).isChecked())
DisplayToast("CheckBox is checked");
else
DisplayToast("CheckBox is unchecked");
}
});RadioButton
:需要使用RadioGroup
的setOnCheckedChangeListener()
方法注册一个回调函数, 以便该组中的被选中的RadioButton
发生变化的时候调用。1
2
3
4
5
6
7
8
9
10
11
12RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rdbGp1);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb1 = (RadioButton) findViewById(R.id.rdb1);
if (rb1.isChecked()) {
DisplayToast("Option 1 checked");
} else {
DisplayToast("Option 2 checked");
}
}
});
列表视图
ListView
,Spinner
ListView
:1
2
3
4
5setListAdapter(new ArrayAdapter<String>(
this,android.R.layout.simple_list_item_checked, presidents));//设置列表项
public void onListItemClick(ListView parent, View v, int position, long id) {
Toast.makeText(this, "you have selected:" + presidents[position], Toast.LENGTH_SHORT).show();
}getListView()
获取当前的列表视图,只有当前Activity
继承ListAcitvity
时才能用。Spinner
:1
2
3
4
5<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:drawSelectorOnTop="true"/>设置列表和判断选择
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, prisidents);
spinner.setAdapter(adapter);//设置列表项
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int index = parent.getSelectedItemPosition();
Toast.makeText(BasicViews6Activity.this, "you have selected item :" + prisidents[index], Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});//判断哪一项被选择了
特殊碎片
ListFragment
,DialogFrament
,PerferenceFragment
要使用ListFragment
首先需要创建一个ListFragment
的类 (fragmnet1
),创建一个关于fragment1
的视图,在 Activity 的 Layout 中声明碎片。在 Activity 中显示于fragment1
的视图,可以使用FragmentManger
和FragmentTransaction
类;1
2
3
4
5
6
7FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
Fragment1 fragment1 = new Fragment1();
fragmentTransaction.replace(android.R.id.content,fragment1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();PerferenceFragment
中PreferenceCategory
用于创建类别
PreferenceScreen
用于显示一个PerferenceFragmnet
界面
练习
- 访问放在 Strings.xml 文件中的字符串资源
getResources()
获取当前日期
1
2
3
4Calendar today = Calendar.getInstance();
yr = today.get(Calendar.YEAR);
month = today.get(Calendar.MONTH);
day = today.get(Calendar.DAY_OF_MONTH);