《RadioButton與ListView的混合使用》一文中,我在適配器中用標(biāo)記的方法實現(xiàn)了用戶選擇的操作,這次用ListView的單選模式來實現(xiàn)一下。ListView的默認(rèn)狀態(tài)下是沒有選擇行為的,把ListView的choiceMode設(shè)置為singleChoice,列表就可以實現(xiàn)單選(當(dāng)然它也有多選模式,這個后面再研究)。
成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),錫山企業(yè)網(wǎng)站建設(shè),錫山品牌網(wǎng)站建設(shè),網(wǎng)站定制,錫山網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,錫山網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。Activity的布局文件如下,ListView選擇了單選模式,這次我把ListView上方的TextView換成了Button:
<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" > <Button android:id="@+id/select" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/select_authors" android:textSize="25sp" /> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:choiceMode="singleChoice" /> </LinearLayout>
ItemList的XML文件,RadioButton換成了CheckBox,另外, CheckBox 是可以獲取焦點的UI控件,為實現(xiàn)ListView的點擊,需要設(shè)置
“ android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"”
這三項,其中,CheckBox的背景選用了自己做的一張圖片,圖片是RadioButton的樣子:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#fff" > <TextView android:id="@+id/author" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:padding="10dp" android:textSize="20sp" /> <CheckBox android:id="@+id/radio" android:layout_width="20dp" android:layout_height="20dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_gravity="center_vertical" android:layout_marginRight="10dp" android:background="@drawable/radio_button_normal" android:button="@null" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" android:padding="10dp" /> </RelativeLayout>
Activity的代碼如下,點擊ListView的Item或者其上方的Button,都可以彈出Toast:
package com.example.choicelistviewtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class RadioButtonListActivity extends Activity {
private ListView radioButtonList;
private RadioAdapter adapter;
// 模擬幾個數(shù)據(jù),作為List的條目
private String[] authors = { "芥川龍之介", "三島由紀(jì)夫", "川端康成", "村上春樹", "東野圭吾",
"張愛玲", "金庸", "錢鐘書", "老舍", "梁實秋", "亨利米勒", "海明威", "菲茲杰拉德", "凱魯亞克",
"杰克倫敦", "小仲馬", "杜拉斯", "福樓拜", "雨果", "巴爾扎克", "莎士比亞", "勞倫斯", "毛姆",
"柯南道爾", "笛福" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choice_list_view_test);
radioButtonList = (ListView) findViewById(R.id.list);
adapter = new RadioAdapter(this, authors);
radioButtonList.setAdapter(adapter);
radioButtonList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(RadioButtonListActivity.this,
"您選擇的作家是:" + authors[arg2], Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.select).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int select = radioButtonList.getCheckedItemPosition();
// INVALID_POSITION 代表無效的位置。有效值的范圍是 0 到當(dāng)前適配器項目數(shù)減 1 。
if (ListView.INVALID_POSITION != select) {
Toast.makeText(RadioButtonListActivity.this,
"您選擇的作家是:" + authors[select], Toast.LENGTH_SHORT)
.show();
} else {
// 如果用戶開始沒有選擇
Toast.makeText(RadioButtonListActivity.this, "請選擇一位作家!",
Toast.LENGTH_SHORT).show();
}
}
});
}
}適配器:
package com.example.choicelistviewtest;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public class RadioAdapter extends BaseAdapter {
private String[] authors;
private Context c;
public RadioAdapter(Context c, String[] authors) {
super();
this.c = c;
this.authors = authors;
}
@Override
public int getCount() {
return authors.length;
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ChoiceListItemView choiceListItemView = new ChoiceListItemView(c, null);
choiceListItemView.setName(authors[arg0]);
return choiceListItemView;
}
}ListView是通過實現(xiàn)Checkable接口來處理單選模式的,這要求Item的視圖實現(xiàn)Checkable接口,創(chuàng)建ChoiceListItemView類來實現(xiàn)該接口,ListView選中某個Item時,會調(diào)用ChoiceListItemView類的setChecked的方法:
package com.example.choicelistviewtest;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ChoiceListItemView extends LinearLayout implements Checkable {
private TextView nameTxt;
private CheckBox selectBtn;
public ChoiceListItemView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.item_list, this, true);
nameTxt = (TextView) v.findViewById(R.id.author);
selectBtn = (CheckBox) v.findViewById(R.id.radio);
}
public void setName(String text) {
nameTxt.setText(text);
}
@Override
public boolean isChecked() {
return selectBtn.isChecked();
}
@Override
public void setChecked(boolean checked) {
selectBtn.setChecked(checked);
//根據(jù)是否選中來選擇不同的背景圖片
if (checked) {
selectBtn.setBackgroundResource(R.drawable.radio_button_checked);
} else {
selectBtn.setBackgroundResource(R.drawable.radio_button_normal);
}
}
@Override
public void toggle() {
selectBtn.toggle();
}
}效果圖:

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當(dāng)前題目:ListView的單選模式-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://chinadenli.net/article44/cejhhe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站營銷、自適應(yīng)網(wǎng)站、網(wǎng)站收錄、網(wǎng)站設(shè)計公司、App設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容