Android實(shí)現(xiàn)動態(tài)添加標(biāo)簽及其點(diǎn)擊事件
在做Android開發(fā)的時候,會遇到動態(tài)添加標(biāo)簽讓用戶選擇的功能,所以自己寫了個例子,運(yùn)行效果圖如下。




標(biāo)簽可以左右滑動進(jìn)行選擇,點(diǎn)擊的時候,會彈出toast提示選擇或者取消選擇了哪個標(biāo)簽。通過動態(tài)添加TextView作為標(biāo)簽,并給TextView設(shè)置背景,通過selector選擇器改變其背景顏色,來確定是否處于選中狀態(tài)。
代碼如下所示:
1、標(biāo)簽的布局文件,我在標(biāo)簽里只設(shè)置了一個TextView
<?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" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@drawable/mark_select" android:enabled="false" android:padding="10dp" android:text="TextView" /> </LinearLayout>
2、在res文件夾下新建drawable文件夾,標(biāo)簽的背景設(shè)為@drawable/mark_select,代碼如下所示:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/mark_notbeselected" android:state_enabled="false"/> <item android:drawable="@drawable/mark_beselected" android:state_enabled="true"/> </selector>
當(dāng)標(biāo)簽處于選中狀態(tài),背景為@drawable/mark_beselected,當(dāng)標(biāo)簽處于未選中狀態(tài),背景為@drawable/mark_notbeselected
其中mark_notbeselected代碼為:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#ffffff" /> <corners android:radius="3dip" /> <stroke android:width="1dip" android:color="#1cb0ba" /> </shape>
mark_beselected代碼為:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#1cb0ba" /> <corners android:radius="3dip" /> <stroke android:width="1dip" android:color="#1cb0ba" /> </shape>
3、主頁面布局文件里包括一個水平滾動條HorizontalScrollView,代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <HorizontalScrollView android:id="@+id/horizontalScrollView1" android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > </LinearLayout> </HorizontalScrollView> </LinearLayout>
4、MainActivity.java代碼如下所示:
public class MainActivity extends Activity {
List<String> list;
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
//添加標(biāo)簽內(nèi)容
list = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
String str = "標(biāo)簽" + i;
list.add(str);
}
//初始化標(biāo)簽
initMarksView();
}
private void initMarksView() {
for (int i = 0; i < list.size(); i++) {
View view = View.inflate(MainActivity.this, R.layout.mark_layout, null);
TextView tv = (TextView) view.findViewById(R.id.textView1);
tv.setText(list.get(i));
tv.setTag(i);
view.setTag(false);
// 設(shè)置view的點(diǎn)擊事件,與onClick中的View一致
//否則需要在onClick中,去findViewById,找出設(shè)置點(diǎn)擊事件的控件進(jìn)行操作
//若不如此,則無法觸發(fā)點(diǎn)擊事件
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView) v.findViewById(R.id.textView1);
Log.i("dxl", "TextView click");
if ((Boolean) v.getTag()) {
v.setTag(false);
tv.setEnabled(false);
Toast.makeText(MainActivity.this, "你取消了選擇標(biāo)簽" + tv.getTag(), Toast.LENGTH_SHORT).show();
} else {
v.setTag(true);
tv.setEnabled(true);
Toast.makeText(MainActivity.this, "你選擇了標(biāo)簽" + tv.getTag(), Toast.LENGTH_SHORT).show();
}
}
});
linearLayout.addView(view);
}
}
}
至此,便實(shí)現(xiàn)了動態(tài)添加表情,并可以處理標(biāo)簽點(diǎn)擊事件的功能。
源代碼下載:Android動態(tài)添加標(biāo)簽及其點(diǎn)擊事件
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android給自定義按鍵添加廣播和通過廣播給當(dāng)前焦點(diǎn)輸入框賦值
這篇文章主要介紹了Android給自定義按鍵添加廣播和通過廣播給當(dāng)前焦點(diǎn)輸入框賦值的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android 讀取sdcard上的圖片實(shí)例(必看)
下面小編就為大家?guī)硪黄狝ndroid 讀取sdcard上的圖片實(shí)例(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
Android?IntentFilter的匹配規(guī)則示例詳解
這篇文章主要為大家介紹了Android?IntentFilter的匹配規(guī)則示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android 照片選擇區(qū)域功能實(shí)現(xiàn)示例
這篇文章主要介紹了Android 照片選擇區(qū)域功能實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Android實(shí)現(xiàn)錄音功能實(shí)現(xiàn)實(shí)例(MediaRecorder)
本篇文章主要介紹了Android實(shí)現(xiàn)錄音的實(shí)例代碼(MediaRecorder),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
解析Android開發(fā)優(yōu)化之:對Bitmap的內(nèi)存優(yōu)化詳解
在Android應(yīng)用里,最耗費(fèi)內(nèi)存的就是圖片資源。而且在Android系統(tǒng)中,讀取位圖Bitmap時,分給虛擬機(jī)中的圖片的堆棧大小只有8M,如果超出了,就會出現(xiàn)OutOfMemory異常。所以,對于圖片的內(nèi)存優(yōu)化,是Android應(yīng)用開發(fā)中比較重要的內(nèi)容2013-05-05

