TextView的使用方法
布局文件中的配置:
1 2 3 4 5 6 <TextView android:id ="@+id/textView1" android:layout_width ="fill_parent" android:layout_height ="wrap_content" android:text ="@string/arthinking" />
在Activity类中的使用:
1 2 TextView textView1 = (TextView)findViewById(R.id.textView1); textView1.setText(R.string.username);
EditText的使用方法
布局文件中的配置:
1 2 3 4 5 <EditText android:id ="@+id/editText1" android:layout_width ="fill_parent" android:layout_height ="wrap_content" />
在Activity类中的使用:
1 EditText editText1 = (EditText)findViewById(R.id.editText1);
Button的使用方法
布局文件中的配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 <Button android:id ="@+id/button1" android:layout_width ="fill_parent" android:layout_height ="wrap_content" /> ```xml **在Activity类中的使用:** ```java 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的创建在这里执行:
1 2 3 4 5 6 @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方法:
1 2 3 4 5 6 7 @Override public boolean onOptionsItemSelected (MenuItem item) { if (item.getItemId() == 2 ){ finish(); } return super .onOptionsItemSelected(item); }
RadioGroup和RadioButton
布局文件的编写:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <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中使用:
1 2 3 RadioGroup radioGroup1 = (RadioGroup)findViewById(R.id.radioGroup1); RadioButton radioButton1 = (RadioButton)findViewById(R.id.femaleButton); RadioButton radioButton2 = (RadioButton)findViewById(R.id. radioButton2);
为RadioGroup设置监听器,使用RadioGroup.OnCheckedChangeListener类
1 2 3 4 5 6 7 8 9 10 11 12 13 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
布局文件的编写:
1 2 3 4 5 6 7 8 9 10 11 12 <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中的使用:
1 2 CheckBox checkBox1 = (CheckBox)findViewById(R.id.checkBox1); CheckBox checkBox2 = (CheckBox)findViewById(R.id.checkBox2);
为多选按钮添加监听器,这里使用CompoundButton.OnCheckedChangeListener
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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) { 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中使用:
1 Toast.makeText(RadioTest.this , "checkBox1" , Toast.LENGTH_SHORT).show();
ProgressBar
在布局文件中编写:
1 2 3 4 5 6 7 <ProgressBar android:id ="@+id/progressBar1" style ="?android:attr/progressBarStyleHorizontal" android:layout_width ="200dp" android:layout_height ="wrap_content" android:visibility ="gone" />
在Activity中使用:
1 ProgressBar progressBar1 = (ProgressBar)findViewById(R.id.progressBar1);
这里使用一个鼠标点击事件触发处理该进度条:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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使用:
主布局文件的编写:
1 2 3 <ListView android:id ="@id/android:list" android:layout_width ="fill_parent" android:layout_height ="wrap_content" android:drawSelectorOnTop ="true" android:scrollbars ="vertical" />
另外需要一个布局文件供ListView使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 <?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的类
1 public class ActivityTest extends ListActivity {…}
在该类中生成ListView:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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方法:
1 2 3 4 5 @Override protected void onListItemClick (ListView l, View v, int position, long id) { super .onListItemClick(l, v, position, id); System.out.println(id); }