萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> android 的 Spinner 下拉菜單的使用

android 的 Spinner 下拉菜單的使用

1.布局文件內容 :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/target_number"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60sp"
        android:layout_marginTop="20sp" >
    </TextView>

    <Spinner
        android:id="@+id/target_pinner"
        android:layout_width="200sp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60sp"
        android:layout_marginTop="20sp" >
    </Spinner>

    <TextView
        android:id="@+id/consult_number"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60sp"
        android:layout_marginTop="20sp" >
    </TextView>

    <Spinner
        android:id="@+id/consult_spinner"
        android:layout_width="200sp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60sp"
        android:layout_marginTop="20sp" >
    </Spinner>

    <Button
        android:id="@+id/manage_operation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="100sp"
        android:layout_marginTop="20sp"
        android:text="執行調度"
        android:textSize="18sp" >
    </Button>

</LinearLayout>

2.JAVA程序代碼:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONObject;

import com.sxzichen.common.MD5Util;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class SameQuen_Activity extends Activity  implements OnClickListener{
    private static final String[] queuinfo={"A01","A02","A03","A04","A05","A06","A07","A08","A09","A10","A11"};
    private TextView target_view ;
    private Spinner target_spinner;
    private ArrayAdapter<String> target_adapter;
    
    private TextView consult_view;
    private Spinner consult_spinner;
    private ArrayAdapter<String> consult_adapter;

    private String targetNumber="";
    private String consultMunber="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_samequeu);
            //定義目標排隊編號下拉菜單
            target_view = (TextView) findViewById(R.id.target_number);
            target_spinner = (Spinner) findViewById(R.id.target_pinner);
            //將可選內容與ArrayAdapter連接起來
            target_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,queuinfo);
            //設置下拉列表的風格
            target_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            //將adapter 添加到spinner中
            target_spinner.setAdapter(target_adapter);
            //添加事件Spinner事件監聽  
            target_spinner.setOnItemSelectedListener(new BooldSpinnerSelectedListener());
            //設置默認值
            target_spinner.setVisibility(View.VISIBLE);
            
            //定義參照排隊編號下拉菜單
            consult_view = (TextView) findViewById(R.id.consult_number);
            consult_spinner = (Spinner) findViewById(R.id.consult_spinner);
            //將可選內容與ArrayAdapter連接起來
            consult_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,queuinfo);
            //設置下拉列表的風格
            consult_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            //將adapter 添加到spinner中
            consult_spinner.setAdapter(consult_adapter);
            //添加事件Spinner事件監聽  
            consult_spinner.setOnItemSelectedListener(new MobleSpinnerSelectedListener());
            //設置默認值
            consult_spinner.setVisibility(View.VISIBLE);
            
            Button manage_operation=(Button)findViewById(R.id.manage_operation); 
            manage_operation.setOnClickListener(this); 
            
    }
    
    //選擇 目標排隊編號 事件 監聽器
    class BooldSpinnerSelectedListener implements OnItemSelectedListener{

            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                            long arg3) {
                      targetNumber=queuinfo[arg2];

                     target_view.setText("要移動的排隊編號是:"+queuinfo[arg2]);
            }

            public void onNothingSelected(AdapterView<?> arg0) {
            }
    }


    //    //選擇 參照排隊編號 事件 監聽器
    class MobleSpinnerSelectedListener implements OnItemSelectedListener{

            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                            long arg3) {
            	         consultMunber=queuinfo[arg2];
            	consult_view.setText("移動到 "
						
copyright © 萬盛學電腦網 all rights reserved