国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實(shí)現(xiàn)動態(tài)添加標(biāo)簽及其點(diǎn)擊事件

2019-10-21 21:34:16
字體:
供稿:網(wǎng)友

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

Android,標(biāo)簽,點(diǎn)擊事件

Android,標(biāo)簽,點(diǎn)擊事件

Android,標(biāo)簽,點(diǎn)擊事件

Android,標(biāo)簽,點(diǎ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í)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 姚安县| 海宁市| 崇信县| 年辖:市辖区| SHOW| 新乡市| 阜平县| 谷城县| 文昌市| 五家渠市| 铜山县| 二连浩特市| 崇阳县| 崇阳县| 安庆市| 集贤县| 阿拉善盟| 板桥市| 吉隆县| 拉萨市| 呼图壁县| 玛沁县| 北碚区| 新营市| 枝江市| 绥芬河市| 绥中县| 铜梁县| 唐山市| 兴宁市| 静宁县| 盐亭县| 南江县| 富蕴县| 鄂托克旗| 闻喜县| 津南区| 德阳市| 平谷区| 施甸县| 旌德县|