一、ProgressBar的使用方法
ProgressBar是一种可视化的操作进度条。告诉用户当前操作的执行进度。横条进度条可以有两种现实,一种深色的,一种浅色的,进度可以分别控制。
示例:一个应用程序有2个ProgressBar,让进度条显示进度
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:text="@string/hello"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<!-- 水平进度条,android:visibility="gone"表示进度条处于不可见状态 -->
<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="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<Button
android:id="@+id/myButton"
android:text="begin"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
public class ProgressBarTest extends Activity{
private ProgressBar firstBar = null;
private ProgressBar secondBar = null;
private Button myButton = null;
private int i = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
firstBar = (ProgressBar) findViewById(R.id.firstBar);
secondBar = (ProgressBar) findViewById(R.id.secondBar);
myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new ButtonListener());
System.out.println(firstBar.getMax());
}
class ButtonListener implements OnClickListener{
public void onClick(View v) {
if(i == 0){
//设置进度条处于可见状态
firstBar.setVisibility(View.VISIBLE);
secondBar.setVisibility(View.VISIBLE);
}else if(i < firstBar.getMax()){
//设置进度条的当前值
firstBar.setProgress(i);
//设置第二进度条的当前值
firstBar.setSecondaryProgress(i + 10);
//因为默认的进度条无法显示进行的状态
//secondBar.setProgress(i);
}else{
//设置进度条处于不可见状态
firstBar.setVisibility(View.GONE);
secondBar.setVisibility(View.GONE);
}
i = i + 10;
}
}
}
二、ListView的使用方法
示例:一个应用程序有一个ListView,显示三行信息
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:text="@string/hello"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<!--
android:drawSelectorOnTop ————> 代表选择器渲染,
true代表选择器将渲染在所选item上,false代表选择器将渲染在所选item之下
android:scrollbars ————> 滚动条的方向
-->
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbars="vertical" />
</LinearLayout>
user.xml
<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="1dip"
android:paddingBottom="1dip" >
<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>
public class ListViewTest extends ListActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
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", "zhangsan");
map1.put("user_ip", "192.168.1.121");
map2.put("user_name", "lisi");
map2.put("user_ip", "192.168.1.122");
map3.put("user_name", "wangwu");
map3.put("user_ip", "192.168.1.123");
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_name,R.id.user_ip});
setListAdapter(listAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
System.out.println("id:" + id);
System.out.println("position:" + position);
}
}