Android中的常用控件及其基本用法
2014-02-20 12:22
453 查看
TextView的使用方法
布局文件中的配置:
在Activity类中的使用:
EditText的使用方法
布局文件中的配置:
在Activity类中的使用:
Button的使用方法
布局文件中的配置:
在Activity类中的使用:
Menu的使用方法
onCreateOptionsMenu(Menu menu)
Initialize the contents of the Activity's standard options menu.
onCreateOptionsMenu是Activity中的一个方法,当用户点击了MENU按钮时,Activity会触发该方法,Menu的创建在这里执行:
为按钮添加方法,需要实现Activity的onOptionsItemSelected方法:
RadioGroup和RadioButton
布局文件的编写:
在Activity中使用:
为RadioGroup设置监听器,使用RadioGroup.OnCheckedChangeListener类
CheckBox
布局文件的编写:
在Activity中的使用:
为多选按钮添加监听器,这里使用CompoundButton.OnCheckedChangeListener
Toast
public class
Toast
extends Object
A toast is a view containing a quick little message for the user. The toast class helps you create and show those.
直接在Activity中使用:
ProgressBar
在布局文件中编写:
在Activity中使用:
这里使用一个鼠标点击事件触发处理该进度条:
ListView
public class
ListView
extends AbsListView
A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view.
要使用ListView必须继承ListActivity类,同时需要在代码中构造 一个android.widget.SimpleAdapter类,用于辅助创建ListView。除此之外还需要创建另外一个布局文件供ListView使用:
主布局文件的编写:
另外需要一个布局文件供ListView使用:
继承ListActivity的类
在该类中生成ListView:
要实现监听事件,可以实现ListActivity的onListItemClick方法:
布局文件中的配置:
<TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/arthinking" />
在Activity类中的使用:
TextView textView1 = (TextView)findViewById(R.id.textView1); textView1.setText(R.string.username);
EditText的使用方法
布局文件中的配置:
<EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_height="wrap_content" />
在Activity类中的使用:
EditText editText1 = (EditText)findViewById(R.id.editText1);
Button的使用方法
布局文件中的配置:
<Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" />
在Activity类中的使用:
Button button1 = (Button)findViewById(R.id. button1); //TestListener为继承OnClickListener的类 button1.setOnClickListener(new TestListener());
Menu的使用方法
onCreateOptionsMenu(Menu menu)
Initialize the contents of the Activity's standard options menu.
onCreateOptionsMenu是Activity中的一个方法,当用户点击了MENU按钮时,Activity会触发该方法,Menu的创建在这里执行:
@Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 1, 1, R.string.back); menu.add(0,2,2,R.string.exit); return super.onCreateOptionsMenu(menu); }
为按钮添加方法,需要实现Activity的onOptionsItemSelected方法:
@Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == 2){ finish(); } return super.onOptionsItemSelected(item); }
RadioGroup和RadioButton
布局文件的编写:
<RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/female" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/male" /> </RadioGroup>
在Activity中使用:
RadioGroup radioGroup1 = (RadioGroup)findViewById(R.id.radioGroup1); RadioButton radioButton1 = (RadioButton)findViewById(R.id.femaleButton); RadioButton radioButton2 = (RadioButton)findViewById(R.id. radioButton2);
为RadioGroup设置监听器,使用RadioGroup.OnCheckedChangeListener类
radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if(radioButton1.getId() == checkedId){ System.out.println("radioButton1"); } else if(radioButton2.getId() == checkedId) { System.out.println("radioButton2"); } } });
CheckBox
布局文件的编写:
<CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/checkBox1" /> <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/checkBox2" />
在Activity中的使用:
CheckBox checkBox1 = (CheckBox)findViewById(R.id.checkBox1); CheckBox checkBox2 = (CheckBox)findViewById(R.id.checkBox2);
为多选按钮添加监听器,这里使用CompoundButton.OnCheckedChangeListener
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { System.out.println("checkBox1 is checked"); } else { System.out.println("checkBox1 is unchecked"); } } }); checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked) { System.out.println("checkBox2 is checked"); } else { System.out.println("checkBox2 is unchecked"); } } });
Toast
public class
Toast
extends Object
A toast is a view containing a quick little message for the user. The toast class helps you create and show those.
直接在Activity中使用:
Toast.makeText(RadioTest.this, "checkBox1", Toast.LENGTH_SHORT).show();
ProgressBar
在布局文件中编写:
<ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="200dp" android:layout_height="wrap_content" android:visibility="gone" />
在Activity中使用:
ProgressBar progressBar1 = (ProgressBar)findViewById(R.id.progressBar1);
这里使用一个鼠标点击事件触发处理该进度条:
private int i = 0; class ButtonListener implements OnClickListener{ @Override public void onClick(View v) { if(i == 0) { progressBar1.setVisibility(View.VISIBLE); progressBar1.setMax(100); } else if ( i < progressBar1.getMax()){ progressBar1.setProgress(i); progressBar1.setSecondaryProgress(i + 10); } else{ progressBar1.setVisibility(View.GONE); } i = i + 10 ; } }
ListView
public class
ListView
extends AbsListView
A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view.
要使用ListView必须继承ListActivity类,同时需要在代码中构造 一个android.widget.SimpleAdapter类,用于辅助创建ListView。除此之外还需要创建另外一个布局文件供ListView使用:
主布局文件的编写:
<ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawSelectorOnTop="true" android:scrollbars="vertical" />
另外需要一个布局文件供ListView使用:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingTop="2dip" android:paddingBottom="2dip"> <TextView android:id="@+id/user_name" android:layout_width="180dip" android:layout_height="30dip" android:textSize="5pt" android:singleLine="true" /> <TextView android:id="@+id/user_id" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="right" android:textSize="5pt" /> </LinearLayout>
继承ListActivity的类
public class ActivityTest extends ListActivity {…}
在该类中生成ListView:
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", "arthinking"); map1.put("user_id", "001"); map2.put("user_name", "Jason"); map2.put("user_id", "002"); list.add(map1); list.add(map2); SimpleAdapter listAdapter = new SimpleAdapter(this, list, R.layout.user, new String[]{"user_name","user_id"}, new int[]{R.id.user_name,R.id.user_id}); setListAdapter(listAdapter);
要实现监听事件,可以实现ListActivity的onListItemClick方法:
@Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); System.out.println(id); }
相关文章推荐
- Android中的常用控件及其基本用法
- Android中的常用控件及其基本用法
- Android中的常用控件及其基本用法
- android基本控件常用的功能及属性
- 安卓学习基础——Android常用基本控件(一)
- Android ListView控件基本用法以及范例
- Android 常用基本控件 ImageButton
- android基础-----Intent控件的基本用法
- Android 基本常用控件ListView的简单应用
- MFC基本常用控件基本用法
- Android ListView控件基本用法
- 【Android成长之路】最常用和最难用的控件——ListView的浅谈(ListView 的简单用法)
- Android第四讲——常用的基本控件(一)TextView、EditText、Button、RadioButton、CheckButton、ImageView
- 【Android】5.0 第5章 常用基本控件--本章示例主界面
- MFC基本常用控件基本用法
- 【Android基本控件】ListView 的用法
- Android开发——控件TextView的用法(常用属性)
- Android笔记---常用控件以及用法
- Android第四讲——常用的基本控件(三)Toast、Dialog、PopupWindows、Notification(通知)、ViewPager
- Android常用控件七之ProgressBar的代码用法