android中利用實現二級聯動的效果

來源:互聯網
上載者:User

本篇文章實現的效果就是中所圈的那樣,實作類別似於HTML中的二級聯動的效果。 對於第一個選項我們讀取的是本地xml檔案來填充資料的, 對於第二個選項我們讀取的是通過中央氣象台提供的API返回的xml格式的資料來填充的。 首先是首頁面排版,由於我做的是一個天氣預報的功能,所以添加了很多與本文無關的控制項,在代碼注釋中寫的很清楚,大家可以直接略過。  

public class WeatherPage extends RelativeLayout{      private Context parentContext;            /**監聽*/      private MyButtonListen mylisten;            /**定義天氣對象*/      private GetWeatherService service;            private RelativeLayout toplayout;      private int toplayoutid=100;      private RelativeLayout centerlayout;      private int centerlayoutid=200;      private RelativeLayout footlayout;      private int footlayoutid=300;            /** topView*/      private ImageView dogpetimg;      private TextView titleview;      private ImageButton switchBtn;            /** showweatherView*/      private RelativeLayout showWeather;      private int showWeatherid=201;      private TextView datetext;            /** functionView*/      private LinearLayout functionLayout;      private TextView selectCitytext;      private Spinner selectProvince;      private ArrayAdapter<CharSequence> adapterProvince;      private Map<String,String> provincemap;            private Spinner selectCity;      private Map<Integer,CityWeather> citymap;      private ArrayAdapter<CharSequence> adapterCity;            private ImageButton okBtn;      private ImageButton selectBtn;            /** 本類對象的樣式 */      private LayoutParams lp;//準備放入ViewFlipper中,所以暫且準備一下,            public WeatherPage(Context context) {          super(context);          this.parentContext=context;          mylisten=new MyButtonListen();          service=new GetWeatherService();          init();      }      private void init() {          toplayout=new RelativeLayout(parentContext);          toplayout.setId(toplayoutid);          LayoutParams lptoplayout=new LayoutParams(-1,-2);          lptoplayout.setMargins(0, 20, 0, 0);          //添加組件          addTopLayout(lptoplayout);          addView(toplayout,lptoplayout);                                        centerlayout=new RelativeLayout(parentContext);          centerlayout.setId(centerlayoutid);          LayoutParams lpcenterlayout=new LayoutParams(-1,370);          lpcenterlayout.setMargins(0, 30, 0, 0);          lpcenterlayout.addRule(RelativeLayout.BELOW,toplayoutid);          //添加組件          addCenterLayout(lpcenterlayout);          addView(centerlayout,lpcenterlayout);                              footlayout=new RelativeLayout(parentContext);          footlayout.setBackgroundColor(Color.RED);          footlayout.setId(footlayoutid);          LayoutParams lpfootlayout=new LayoutParams(-1,-2);          lpfootlayout.setMargins(20, 10, 20, 0);          //添加組件          addFootLayout(lpfootlayout);          lpfootlayout.addRule(RelativeLayout.BELOW,centerlayoutid);          addView(footlayout,lpfootlayout);                }            public LayoutParams getLp() {          this.lp=new LayoutParams(-1,-1);          return lp;      }            public void addTopLayout(LayoutParams lp){          dogpetimg=new ImageView(parentContext);          LayoutParams lpdogpetimg=new LayoutParams(60,60);          lpdogpetimg.addRule(RelativeLayout.ALIGN_PARENT_LEFT);          dogpetimg.setBackgroundResource(R.drawable.dogsmall);          lpdogpetimg.setMargins(10, 0, 0, 0);                    titleview=new TextView(parentContext);          titleview.setText("天氣預報");          titleview.setTextColor(Color.BLUE);          LayoutParams lptitleview=new LayoutParams(-2,-2);          lptitleview.addRule(RelativeLayout.CENTER_HORIZONTAL);                    switchBtn=new ImageButton(parentContext);          //先進行判斷,判斷原來的開關狀態,然後再添加背景圖片,標記位設定在helper類中。          switchBtn.setBackgroundResource(R.drawable.start);          LayoutParams lpswitchBtn=new LayoutParams(40,40);          lpswitchBtn.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);          lpswitchBtn.setMargins(0, 20, 50, 0);          //添加監聽          switchBtn.setOnClickListener(mylisten);            toplayout.addView(dogpetimg,lpdogpetimg);          toplayout.addView(titleview, lptitleview);          toplayout.addView(switchBtn,lpswitchBtn);                }        public void addCenterLayout(LayoutParams lp) {          showWeather=new RelativeLayout(parentContext);          showWeather.setId(showWeatherid);          LayoutParams lpshowWeather=new LayoutParams(400,300);          lpshowWeather.addRule(RelativeLayout.CENTER_HORIZONTAL);          showWeather.setBackgroundColor(Color.CYAN);                    datetext=new TextView(parentContext);          LayoutParams lpdatetext=new LayoutParams(400,-2);          lpdatetext.addRule(RelativeLayout.CENTER_HORIZONTAL);          lpdatetext.addRule(RelativeLayout.BELOW,showWeatherid);          lpdatetext.setMargins(20, 20, 20, 0);  //      datetext.setBackgroundColor(Color.LTGRAY);          datetext.setText(TimeHelper.getDateInChina());          datetext.setGravity(Gravity.CENTER_HORIZONTAL);                    centerlayout.addView(showWeather, lpshowWeather);          centerlayout.addView(datetext, lpdatetext);      }        public void addFootLayout(LayoutParams lp) {                    functionLayout=new LinearLayout(parentContext);          functionLayout.setId(301);  //      functionLayout.setBackgroundColor(Color.YELLOW);          LayoutParams lpfunctionLayout=new LayoutParams(-2,-2);          lpfunctionLayout.addRule(RelativeLayout.ALIGN_PARENT_TOP);          lpfunctionLayout.addRule(RelativeLayout.CENTER_HORIZONTAL);          lpfunctionLayout.setMargins(10, 0, 0, 0);                    //添加顯示文字          selectCitytext=new TextView(parentContext);          selectCitytext.setText("請設定:");                    //添加選擇省          selectProvince=new Spinner(parentContext);          selectProvince.setPrompt("請選擇省份");          //擷取省份Map<序號,省份對象>          provincemap=service.getProvinceMap();          String[] provinceData=service.getProvinceArray(provincemap);          //定義下拉式清單適配器,用於填充內容          adapterProvince=null;          adapterProvince=new ArrayAdapter<CharSequence>(parentContext,android.R.layout.simple_spinner_item,provinceData);          //設定列表顯示風格          adapterProvince.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);          selectProvince.setAdapter(adapterProvince);          selectProvince.setOnItemSelectedListener(new MyOnItemSelectedListen());                    //添加選擇市          selectCity=new Spinner(parentContext);          selectCity.setPrompt("請選擇城市");          //定義下拉式清單適配器,用於填充內容          adapterCity=null;          //設定列表顯示風格          selectCity.setAdapter(adapterCity);                    functionLayout.addView(selectCitytext);          functionLayout.addView(selectProvince);          functionLayout.addView(selectCity);                    okBtn=new ImageButton(parentContext);          okBtn.setBackgroundResource(R.drawable.okbtn);//給綁定按鈕添加背景圖片          LayoutParams lpokBtn=new LayoutParams(-2,-2);          lpokBtn.setMargins(20, 20, 0, 0);          lpokBtn.addRule(RelativeLayout.ALIGN_PARENT_LEFT);          lpokBtn.addRule(RelativeLayout.BELOW,301);          //添加監聽          okBtn.setOnClickListener(mylisten);                    selectBtn=new ImageButton(parentContext);          selectBtn.setBackgroundResource(R.drawable.selectbn);//給查詢按鈕添加背景圖片          LayoutParams lpselectBtn=new LayoutParams(-2,-2);          lpselectBtn.setMargins(0, 20, 20, 0);          lpselectBtn.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);          lpselectBtn.addRule(RelativeLayout.BELOW,301);          //添加監聽          selectBtn.setOnClickListener(mylisten);                    footlayout.addView(functionLayout,lpfunctionLayout);          footlayout.addView(okBtn, lpokBtn);          footlayout.addView(selectBtn, lpselectBtn);      }        /**      * 監聽各種按鈕的點擊事件      * @author Administrator      */      class MyButtonListen implements OnClickListener{          @Override          public void onClick(View v) {              // TODO Auto-generated method stub              if(v==switchBtn){                  //關閉外顯示功能                  System.out.println("點擊外顯示開關");                  //換色                  switchBtn.setBackgroundResource(R.drawable.end);              }else if(v==okBtn){                  //確定,對城市進行儲存。寫入小型資料庫                  System.out.println("點擊儲存開關");                                }else if(v==selectBtn){                  //確定,對城市進行查詢,不儲存                  System.out.println("點擊查詢開關");              }else{                                    Log.e("tag", "問題,輸入的值不對");              }          }      }      /**      * 監聽第一個選項的選擇,當地一個選項框被選擇資料的時候出發該類中的事件      * @author Administrator      */      class MyOnItemSelectedListen implements OnItemSelectedListener{            @Override          public void onItemSelected(AdapterView<?> item, View view,                  int position, long arg3) {                            String provincename = item.getAdapter().getItem(position).toString();              System.out.println(provincename);              String pinyin = provincemap.get(provincename);              Map<String, String> cityIdMap = service.getCityMap(pinyin);              String[] cityArray = service.getCityArray(cityIdMap);                            adapterCity=new ArrayAdapter<CharSequence>(parentContext,android.R.layout.simple_spinner_item,cityArray);              //設定列表顯示風格              adapterCity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);              selectCity.setAdapter(adapterCity);          }            @Override          public void onNothingSelected(AdapterView<?> arg0) {              // TODO Auto-generated method stub                        }      }  }  

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.