当前位置:操作系统 > 安卓/Android >>

android中利用实现二级联动的效果

本篇文章实现的效果就是如图中所圈的那样,实现类似于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.易做图_spinner_item,provinceData);  
        //设置列表显示风格  
        adapterProvince.setDropDownViewResource(android.R.layout.易做图_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);//
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,