自定义listview和ProgressBar的简单使用
生活随笔
收集整理的這篇文章主要介紹了
自定义listview和ProgressBar的简单使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?自定義listview和ProgressBar的簡單使用
先看效果圖
?
通過“+”和”-”按鈕來控制ProgressBar的進度,最大刻度為100,每次點擊按鈕進度加10或減10,當刻度超過100的時候刻度會從0重新開始。
?
MainActivity:
?
package com.example.zml4;import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast;/*** @author Bri* */ public class MainActivity extends Activity implements OnClickListener {ProgressBar pro;Button bt_plus, bt_sub, bt_next;TextView tv_show;int progress = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);pro = (ProgressBar) findViewById(R.id.progressBar1);bt_plus = (Button) findViewById(R.id.bt_plus);bt_sub = (Button) findViewById(R.id.bt_sub);bt_next = (Button) findViewById(R.id.bt_next);tv_show = (TextView) findViewById(R.id.tv_s);pro.setMax(100);pro.setProgress(progress);bt_plus.setOnClickListener(this);bt_sub.setOnClickListener(this);bt_next.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.bt_plus:progress += 10;if (progress > 100) {progress = 0;pro.setProgress(progress);} else {pro.setProgress(progress);}tv_show.setText(progress + "%");break;case R.id.bt_sub:progress -= 10;if (progress < 0) {progress = 0;pro.setProgress(progress);} else {pro.setProgress(progress);}tv_show.setText(progress + "%");break;case R.id.bt_next:Intent intent = new Intent(MainActivity.this, ListViewRatingBarActivity.class);startActivity(intent);break;}}public void author(View v) {Toast.makeText(this, "zml2015\n軟件工程\nQQ:107****340", Toast.LENGTH_SHORT).show();} }?
?
?布局文件:
?
?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.zml4.MainActivity" ><TextViewandroid:id="@+id/tv_s"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="0%" ></TextView><ProgressBarandroid:id="@+id/progressBar1"style="?android:attr/progressBarStyleHorizontal"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_marginTop="22dp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center" ><Buttonandroid:id="@+id/bt_plus"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="+" ></Button><Buttonandroid:id="@+id/bt_sub"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="-" ></Button></LinearLayout><Buttonandroid:id="@+id/bt_next"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginTop="50dp"android:text="ListViewRatingBar" /><TextViewandroid:id="@+id/tv_author"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="20dp"android:clickable="true"android:gravity="center"android:onClick="author"android:text="點我~\(≧▽≦)/~" /></LinearLayout>?
?
?
?
?
制作動態ListView,當RatingBar的星號數大于2顆的時候,名字變為大寫,反之小于等于2顆星的時候變為小寫
自定義Listview
?
package com.example.zml4;import android.app.Activity; import android.os.Bundle; import android.widget.ListView;/*** @author Bri* */ public class ListViewRatingBarActivity extends Activity {ListView lv;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.listview);lv = (ListView) findViewById(R.id.lv);String name[] = { "aa", "BB", "cFc", "DeD", "EE", "ff", "GG", "Ahh","bEE", "Eff", "fGG", "aa", "BB", "cc", "DD", "EE", "ff", "GG","hh", "EE", "ff", "GG" };lv.setAdapter(new MyAdapter(this, name));} }
自定義的適配器:
?
?
package com.example.zml4;import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListAdapter; import android.widget.RatingBar; import android.widget.RatingBar.OnRatingBarChangeListener; import android.widget.TextView; import android.widget.Toast;/*** @author Bri* */ public class MyAdapter extends BaseAdapter implements ListAdapter {String data[];LayoutInflater inflater;Context context;public MyAdapter(Context context, String[] data) {this.data = data;inflater = LayoutInflater.from(context);this.context = context;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn data.length;}@Overridepublic Object getItem(int arg0) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int arg0) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getView(int position, View view, ViewGroup arg2) {final int pos = position;view = inflater.inflate(R.layout.listview_item, null);final TextView tv = (TextView) view.findViewById(R.id.tv_name);final RatingBar rt = (RatingBar) view.findViewById(R.id.rb);rt.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {@Overridepublic void onRatingChanged(RatingBar arg0, float arg1, boolean arg2) {String show;arg0.setRating(arg1);Toast.makeText(context, "星星數:" + arg1, 0).show();if (arg1 > 2) {show = data[pos].toUpperCase();} else {show = data[pos].toLowerCase();}tv.setText(show);Log.e("num", "星星數:" + arg1);}});if (tv.getText().equals("name"))tv.setText(data[pos]);return view;}// 懶得寫優化了,就不寫了static class ViewHolder {RatingBar rb;TextView tv;public ViewHolder(RatingBar rb, TextView tv) {this.rb = rb;this.tv = tv;}} }?
?
自定義ListView的布局及其子布局
?
<?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" ><ListView android:id="@+id/lv"android:layout_width="match_parent"android:layout_height="match_parent"></ListView> </LinearLayout>?
?
?
<?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" android:gravity="center"><TextViewandroid:id="@+id/tv_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="name"/><RatingBar android:id="@+id/rb"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>?
完整源碼下載
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的自定义listview和ProgressBar的简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿联酋选出首位 AI 国务部长(附You
- 下一篇: 全网最详细中英文ChatGPT-GPT-