Spinner(2): 動態添加資源

來源:互聯網
上載者:User

本篇部落格是基於上篇部落格http://blog.csdn.net/androidbluetooth/article/details/6605223所寫。

1. main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:padding="10dip"    android:layout_width="fill_parent"    android:layout_height="wrap_content">    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginTop="10dip"        android:text="@string/planet_prompt"    />    <Spinner         android:id="@+id/spinner"        android:layout_width="fill_parent"        android:layout_height="wrap_content"    /></LinearLayout>

2. Activity代碼

package mark.zhang;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.Toast;public class SpinnerUsedResourcesActivity extends Activity {private int counter = 0;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Spinner spinner = (Spinner) findViewById(R.id.spinner);/* R.layout.simple是自訂布局, spinner顯示資源在該布局上面顯示 */ArrayAdapter<String> adapter = new ArrayAdapter<String>(SpinnerUsedResourcesActivity.this, R.layout.simple);/* 添加顯示資源 */for (int i = 0; i < 5; i++) {adapter.add("linux" + i);}adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);spinner.setAdapter(adapter);spinner.setOnTouchListener(new MyOnTouchListener());}/** * @author mark 內部類 */public class MyOnTouchListener implements OnTouchListener {@Overridepublic boolean onTouch(View v, MotionEvent event) {counter++;System.out.println(counter);if (counter % 2 == 1) { // 觸摸次數是奇數Toast.makeText(SpinnerUsedResourcesActivity.this,"you touch this " + counter + "-th times",Toast.LENGTH_LONG).show();}return false;}}}

3. 自訂布局檔案simple.xml

<?xml version="1.0" encoding="utf-8"?><!-- android:ellipsize="marquee" 當文字過長以跑馬燈形式顯示 --><TextView xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@android:id/text1"style="?android:attr/spinnerItemStyle"    android:singleLine="true"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="center"    android:ellipsize="marquee" />

4. 說明

與上篇部落格http://blog.csdn.net/androidbluetooth/article/details/6605223不同點:

<1> spinner的顯示資源是在代碼中動態添加的。

<2> spinner綁定的事件是OnTouchListener(觸摸spinner本身觸發該事件),不是OnItemSelectedListener(選擇顯示資源某個選項是觸發該事件)。

<3> 建立 ArrayAdapter 對象的方式不同。

5. 思考

根據上面對觸摸事件的計數counter,我們可以在點擊事件中判斷點擊次數。如 OnClick 事件。

findViewById(R.id.test).setOnClickListener(new OnClickListener() {public void onClick(View v) {counter ++;Log.d("mark", "counter = " + counter);}});

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.