Android学习笔记-常用控件
来源:互联网 发布:新加坡李家王朝 知乎 编辑:程序博客网 时间:2024/06/05 06:23
单选按钮 Radio
<RadioGroup android:id="@+id/genderGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <RadioButton android:id="@+id/femaleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/female"/> <RadioButton android:id="@+id/maleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/male"/> </RadioGroup>
genderGroup = (RadioGroup) findViewById(R.id.genderGroup);maleButton = (RadioButton) findViewById(R.id.maleButton);femaleButton = (RadioButton) findViewById(R.id.femaleButton);//...genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubif (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
<CheckBox android:id="@+id/swim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/genderGroup" android:text="@string/swim"/> <CheckBox android:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/swim" android:text="@string/read"/> <CheckBox 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() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {// TODO Auto-generated method stubif (isChecked) {System.out.println("Swim is checked");} else {System.out.println("Swim is unchecked");}}});readBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {// TODO Auto-generated method stubif (isChecked) {System.out.println("Read is checked");} else {System.out.println("Read is unchecked");}}});runBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {// TODO Auto-generated method stubif (isChecked) {System.out.println("Run is checked");} else {System.out.println("Run is unchecked");}}});}
进度条 ProgressBar
<ProgressBar android:id="@+id/firstBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="200dp" android:layout_height="wrap_content" android:visibility="gone"/><ProgressBar 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"/><Button android:id="@+id/myButton" 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;@Overrideprotected 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{@Overridepublic void onClick(View v) {if (i == 0) {firstBar.setVisibility(View.VISIBLE);secondBar.setVisibility(View.VISIBLE);}else if (i < firstBar.getMax()) {//设置朱进度条的值firstBar.setProgress(i);//设置第二进度条的值secondBar.setSecondaryProgress(i + 10);//默认的进度条无法显示进行的状态//secondBar.setProgress(i);}else {firstBar.setVisibility(View.GONE);secondBar.setVisibility(View.GONE);}i = i + 10;}}}
列表 ListView
main.xml
<?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" > <LinearLayout android:id="@+id/ListLinearLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ListView android:id="@+id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawSelectorOnTop="false" android:scrollbars="vertical"/> </LinearLayout></LinearLayout>
user.xml
<?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="horizontal" > <TextView android:id="@+id/user_name" android:layout_width="180dip" android:layout_height="30dip" android:textSize="10pt" android:singleLine="true"/> <TextView android:id="@+id/user_ip" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="10pt" android:gravity="right"/> </LinearLayout>
MainActivity.java
public class MainActivity extends ListActivity{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);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", "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);}@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {// TODO Auto-generated method stubsuper.onListItemClick(l, v, position, id);System.out.println("id:" + id);System.out.println("position:" + position);}}
本文出自 “优赛工作室” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1579162
0 0
- Android学习笔记-常用控件
- Android 常用控件 学习笔记
- [学习笔记]Android常用控件
- android学习笔记之一常用控件
- android学习笔记之一常用控件
- Android学习笔记三常用控件
- Android学习笔记5:常用控件(1)
- Android学习笔记_30_常用控件使用
- Android学习笔记之常用控件
- Android 学习笔记之常用控件
- Android 常用控件(三)学习笔记
- Android Material Design常用控件学习笔记
- Android学习笔记:<二>03.Android当中的常用控件
- Android常用控件学习
- Android学习笔记7——常用控件
- Android学习笔记3——四种常用控件
- Android学习笔记7——常用控件ProgressBar
- Android学习笔记8——常用控件ListView
- Android学习笔记-Activity&Intent
- Android学习笔记-EditText&TextView&Button&菜单栏
- 笔试题目记录
- Android学习笔记-Activity的生命周期
- Android学习笔记-Activity的布局
- Android学习笔记-常用控件
- Android学习笔记-Handler的使用
- Android学习笔记-SQLite的使用
- Spring4学习笔记-SpringJDBC基本操作(补充)
- Android学习笔记-文件下载
- Android学习笔记-广播机制
- Android学习笔记-ContentProvider
- Android学习笔记-XML处理
- Android学习笔记-Wifi网络操作