Android常用控件——10

<pre name="code" class="java"><pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;">ProgressBar</span>
 

 
</pre><pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
   android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    
    <!-- 
    	style表示进度条的外观,有两种
    	"?android:attr/progressBarStyleHorizontal"表示水平进度条
    	"?android:attr/progressBarStyle"表示默认风格(圆圈)外观进度条
    	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="200dp" 
    	android:layout_height="wrap_content" 
    	android:visibility="gone" />

</LinearLayout>


主代码

package me.bym;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class ProgressBarTest extends Activity {
	
	private ProgressBar firstBar = null;
	private ProgressBar secondBar = null;
	private Button startButton = null;
	
	private int i = 0; // 声明进度条计数状态值
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        firstBar = (ProgressBar) findViewById(R.id.firstBar);
        secondBar = (ProgressBar) findViewById(R.id.secondBar);
        startButton = (Button) findViewById(R.id.myButton);
        
        startButton.setOnClickListener(new ButtonListener());
    }
    
    class ButtonListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (0 == i) {
				// 将bar设置为可见
				firstBar.setVisibility(View.VISIBLE);
				// 设置bar的最大显示值(范围)
				// 也可在配置文件中ProgressBar设置属性:
				// android:max="maxValue"
				firstBar.setMax(100);
				secondBar.setVisibility(View.VISIBLE);
			} else if (i < firstBar.getMax()) {
				// 设置bar的显示的进度值
				firstBar.setProgress(i);
				firstBar.setSecondaryProgress(i + 10);
				// 由于默认的bar不显示进度值
				// secondBar.setProgress(i);
			} else {
				firstBar.setVisibility(View.GONE);
				secondBar.setVisibility(View.GONE);
			}
			i += 10;
		}
    	
    }
}

Listview

在xml文件中设置

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout android:id="@+id/listLinearLayout" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:orientation="vertical">
    
    <!-- 
         android:id="@+id/android:list" —— 使用android的内置id
         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>
</LinearLayout>

给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="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:gravity="right" 
        android:textSize="10pt" />
    
</LinearLayout>

主程序


package me.bym;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
/*
 * 要实现一个有ListView的Activity,则它应该是继承自ListActivity
 * ListActivity是Activity的一个子类。
 */
public class ListViewTest extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public 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", "zhangsan");
        map1.put("user_ip", "192.168.0.1");
        map2.put("user_name", "lisi");
        map2.put("user_ip", "192.168.0.2");
        map3.put("user_name", "wangwu");
        map3.put("user_ip", "192.168.0.3");
        
        list.add(map1);
        list.add(map2);
        list.add(map3);
        
        /*
         * 将ArrayList中的内容绑定到ListView上。
         * public SimpleAdapter (
         * Context context,   // view所在的上下文(activity)
         * List<? extends Map<String, ?>> data, // 存放要显示内容的数组
         * int resource,  // 显示内容对应的布局资源文件
         * String[] from,  // 一个列名的数组,不显示,用来和map中的key对应
         * int[] to // 为from数组中的每一个标题对应的列指定显示的控件。
         * )
         */
        SimpleAdapter listAdapter = new SimpleAdapter(this, list, 
        		R.layout.users, new String[] {"user_name", "user_ip"},
        		new int[] {R.id.user_name, R.id.user_ip});
        setListAdapter(listAdapter);
    }
    /* 要实现点击ListView上的Item完成某些操作,需重写ListActivity的onListItemClick方法。
     * protected void onListItemClick (
     * ListView l, // 发生点击事件的ListView
     * View v,  // ListView中被点击的View(控件)
     * int position,  // 被点击的View在ListView中的位置
     * long id // 被点击的View的所在row的id
     * )
     */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Toast.makeText(this, l.getId() + "" + v.getId() + "at:/n" +
				"position: " + position + " row id: " + id, Toast.LENGTH_SHORT).show();
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值