RadioGroup

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   android:orientation="vertical" >
<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"

    >
    <RadioButton
        android:id="@+id/r1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="资讯"
        android:layout_weight="1"

        android:button="@null"
         android:gravity="center"
      android:textSize="25dp"
        />
    <RadioButton
        android:id="@+id/r2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="热点"
        android:layout_weight="1"
        android:button="@null"
       android:textSize="25dp"
         android:gravity="center"
        />
    <RadioButton
        android:id="@+id/r3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="博客"
        android:layout_weight="1"
        android:button="@null"
         android:gravity="center"
        android:textSize="25dp"

        />
     <RadioButton
         android:id="@+id/r4"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="推荐"
         android:button="@null"
         android:gravity="center"
         android:textSize="25dp"
         android:layout_weight="1"
         />

</RadioGroup>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:id="@+id/lin"
    >    

</LinearLayout>
<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>
package com.bawei.yuekao;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements OnClickListener {

    private RadioButton r1;
    private RadioButton r2;
    private RadioButton r3;
    private RadioButton r4;
    private LinearLayout lin;
    private ViewPager viewPager;
    int width;
    private String[] url = new String[] {
            "http://www.oschina.net/action/api/news_list?catalog=1&pageIndex=",
            "http://www.oschina.net/action/api/news_list?catalog=4&show=week&pageIndex=",
            "http://www.oschina.net/action/api/blog_list?type=latest&pageIndex=",
            "http://www.oschina.net/action/api/blog_list?type=recommend&pageIndex=" };
    private List<Fragment> list;
    private List<TextView> l;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         //获取屏幕宽度
          width = getWindowManager().getDefaultDisplay().getWidth();

        r1 = (RadioButton) findViewById(R.id.r1);
        r2 = (RadioButton) findViewById(R.id.r2);
        r3 = (RadioButton) findViewById(R.id.r3);
        r4 = (RadioButton) findViewById(R.id.r4);
        r1.setOnClickListener(this);
        r2.setOnClickListener(this);
        r3.setOnClickListener(this);
        r4.setOnClickListener(this);
        lin = (LinearLayout) findViewById(R.id.lin);
        //游标
        l = new ArrayList<TextView>();
        for(int i = 0; i < url.length; i++){
        TextView tv_line = new TextView(MainActivity.this);
            RadioButton button1=new RadioButton(MainActivity.this);

        LinearLayout.LayoutParams params_2 = new LayoutParams(
                width / 4, LayoutParams.WRAP_CONTENT);
        tv_line.setBackgroundColor(Color.GREEN);
        tv_line.setVisibility(View.INVISIBLE);
        l.add(tv_line);
       lin.addView(tv_line,i,params_2);

        }

        //viewpager适配
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setdata();
        FragmentManager fm = getSupportFragmentManager();
        viewPager.setAdapter(new MyViewPager(fm, list));
        //首次进入
        viewPager.setCurrentItem(0);
        r1.setTextColor(Color.GREEN);
        l.get(0).setVisibility(View.VISIBLE);
        //viewpager监听
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                switch (arg0) {
                case 0:
                    r1.setTextColor(Color.GREEN);
                    r2.setTextColor(Color.BLACK);
                    r3.setTextColor(Color.BLACK);
                    r4.setTextColor(Color.BLACK);
                    l.get(0).setVisibility(View.VISIBLE);
                    l.get(1).setVisibility(View.INVISIBLE);
                    l.get(2).setVisibility(View.INVISIBLE);
                    l.get(3).setVisibility(View.INVISIBLE);
                    break;
                case 1:
                    r2.setTextColor(Color.GREEN);
                    r1.setTextColor(Color.BLACK);
                    r3.setTextColor(Color.BLACK);
                    r4.setTextColor(Color.BLACK);
                    l.get(1).setVisibility(View.VISIBLE);
                    l.get(0).setVisibility(View.INVISIBLE);
                    l.get(2).setVisibility(View.INVISIBLE);
                    l.get(3).setVisibility(View.INVISIBLE);
                    break;
                case 2:
                    r3.setTextColor(Color.GREEN);
                    r2.setTextColor(Color.BLACK);
                    r1.setTextColor(Color.BLACK);
                    r4.setTextColor(Color.BLACK);
                    l.get(2).setVisibility(View.VISIBLE);
                    l.get(1).setVisibility(View.INVISIBLE);
                    l.get(0).setVisibility(View.INVISIBLE);
                    l.get(3).setVisibility(View.INVISIBLE);
                    break;
                case 3:
                    r4.setTextColor(Color.GREEN);
                    r2.setTextColor(Color.BLACK);
                    r3.setTextColor(Color.BLACK);
                    r1.setTextColor(Color.BLACK);
                    l.get(3).setVisibility(View.VISIBLE);
                    l.get(1).setVisibility(View.INVISIBLE);
                    l.get(2).setVisibility(View.INVISIBLE);
                    l.get(0).setVisibility(View.INVISIBLE);
                    break;

                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });
    }

    private void setdata() {
        list = new ArrayList<Fragment>();
        for (int i = 0; i < url.length; i++) {
            Fragment1 f1 = new Fragment1(url[i],i);
            list.add(f1);
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.r1:
            viewPager.setCurrentItem(0);
            break;
        case R.id.r2:
            viewPager.setCurrentItem(1);
            break;
        case R.id.r3:
            viewPager.setCurrentItem(2);
            break;
        case R.id.r4:
            viewPager.setCurrentItem(3);
            break;

        }
    }

}
package com.bawei.yuekao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.bawei.vo.Good;
import com.bawei.vo.Goods;
import com.bawei.vo.Myblog;
import com.bawei.vo.Myblogs;
import com.bawei.vo.Mynews;
import com.example.xlistviews.XListView;
import com.example.xlistviews.XListView.IXListViewListener;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.thoughtworks.xstream.XStream;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class Fragment1 extends Fragment implements IXListViewListener {
    String url;
    int m;

    public Fragment1(String url, int i) {
        // TODO Auto-generated constructor stub
        this.url = url;
        this.m = i;
    }

    int num = 0;
    int z = 6;
    int p = 6;
    private BaseAdapter adapter;
    private BaseAdapter adapters;
    private XListView my_xlist;
    List<Mynews> list = new ArrayList<Mynews>();
    List<Myblog> lists = new ArrayList<Myblog>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.f1, null);
        my_xlist = (XListView) view.findViewById(R.id.my_xlist);
        my_xlist.setPullLoadEnable(true);
        my_xlist.setPullRefreshEnable(true);
        my_xlist.setXListViewListener(this);
        shuain();
        return view;
    }

    private void shuain() {
        z = z - 1;
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpMethod.POST, url + z, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                // TODO Auto-generated method stub
                String result = arg0.result;
                XStream stream = new XStream();
                if (m == 0 || m == 1) {
                    stream.processAnnotations(Good.class);
                    Good good = (Good) stream.fromXML(result);
                    List<Mynews> news = good.getNewslist().getNews();
                    list = news;
                    adapter = new Adpter(getActivity(), list);
                    my_xlist.setAdapter(adapter);
                }
                if (m == 2 || m == 3) {
                    stream.processAnnotations(Goods.class);
                    Goods good = (Goods) stream.fromXML(result);
                    List<Myblog> blog = good.getBlogs().getBlog();
                    lists = blog;
                    adapters = new Adrters(getActivity(), lists);
                    my_xlist.setAdapter(adapters);
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        // TODO Auto-generated method stub
        shuain();
        onLoad();
    }

    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub
        p = p - 1;
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpMethod.POST, url + p, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                // TODO Auto-generated method stub
                String result = arg0.result;
                XStream stream = new XStream();
                if (m == 0 || m == 1) {
                    stream.processAnnotations(Good.class);
                    Good good = (Good) stream.fromXML(result);
                    List<Mynews> news = good.getNewslist().getNews();
                    list.addAll(news);
                    adapter.notifyDataSetChanged();
                }
                if (m == 2 || m == 3) {
                    stream.processAnnotations(Goods.class);
                    Goods good = (Goods) stream.fromXML(result);
                    List<Myblog> blog = good.getBlogs().getBlog();
                    lists.addAll(blog);
                    adapters.notifyDataSetChanged();
                }
            }
        });
        onLoad();
    }

    /*
     *
     * 设置时间
     */
    @SuppressLint("SimpleDateFormat")
    private void onLoad() {
        my_xlist.stopRefresh();
        my_xlist.stopLoadMore();
        // 设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前系统时间
        String nowTime = df.format(new Date(System.currentTimeMillis()));
        // 释放时提示正在刷新时的当前时间
        my_xlist.setRefreshTime(nowTime);
    }
}
package com.bawei.yuekao;

import java.util.List;

import com.bawei.vo.Mynews;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class Adpter extends BaseAdapter {
    Context context;
    List<Mynews> list;
    public Adpter(Context context, List<Mynews> list) {
        // TODO Auto-generated constructor stub
    this.context=context;
    this.list=list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder viewHolder;
        if(convertView==null){
        convertView=View.inflate(context, R.layout.xlistview, null);
        viewHolder=new ViewHolder();
        viewHolder.t1=(TextView) convertView.findViewById(R.id.t1);
        viewHolder.t2=(TextView) convertView.findViewById(R.id.t2);
        viewHolder.t3=(TextView) convertView.findViewById(R.id.t3);
        convertView.setTag(viewHolder);
        }else{
        viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.t1.setText(list.get(position).getTitle());
        viewHolder.t2.setText(list.get(position).getBody());
        viewHolder.t3.setText(list.get(position).getPubDate());

        return convertView;
    }
class ViewHolder{
    TextView t1;
    TextView t2;
    TextView t3;

}
}
时间: 2024-10-05 19:51:55

RadioGroup的相关文章

Android RadioGroup和RadioButton详解

实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGroup的情况下,RadioButton可以全部都选中:当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个.并用setOnCheckedChangeListener来对单选按钮进行监听 1 RadioGroup相关属性: 2 RadioGroup.getC

兔子-RadioButton和RadioGroup的关系

RadioButton和RadioGroup的关系: 1.RadioButton表示单个圆形单选框.而RadioGroup是能够容纳多个RadioButton的容器 2.每一个RadioGroup中的RadioButton同一时候仅仅能有一个被选中 3.不同的RadioGroup中的RadioButton互不相干.即假设组A中有一个选中了,组B中依旧能够有一个被选中 4.普通情况下,一个RadioGroup中至少有2个RadioButton 5.普通情况下.一个RadioGroup中的Radio

Spinner、CheckBox、RadioGroup的简单使用

直接上代码吧! .xml的代码如下: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="fill_parent" 5 android:layout_height="wr

Android控件(2)RadioButton&amp;RadioGroup

抄自: http://www.cnblogs.com/wt616/archive/2011/06/20/2085531.html 学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握RadioGroup选中状态变换的事件(监听器) RadioButton和CheckBox的区别: 1.单个RadioButton在选中后,通过点击无法变为未选中 单个Che

android中RadioGroup、RadioButton、Spinner、EditText用法详解(含示例截图和源代码)

为了保护版权.尊重原创,转载请注明出处:http://blog.csdn.net/u013149325/article/details/43237757,谢谢! 今天在项目中用到了android中常用的RadioGroup.RadioButton.Spinner.EditText等控件,在此介绍一下它们的用法,希望对需要的朋友有帮助. 一.RadioGroup和RadioButton的使用 RadioButton就是我们常见的单选按钮,一个RadioGroup可以包含多个单选按钮,但是每次只能选

Fragment + TabHost + RadioGroup

1. 使用FragMent是因为 4.0.3之后 ,摒弃了TabActivity这种用法, Demo 效果图: 先上布局XML R.layout.activity_main [html] view plaincopy <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android"

Android RadioGroup的RadioButton 选择改变字体颜色和背景颜色

RadioGroup <RadioGroup android:id="@+id/client_charge_radiogroup" android:layout_width="200dp" android:layout_height="40dp" android:layout_marginLeft="5dp" android:layout_alignParentRight="true" android

如何获取listview里面的edittext或者RadioGroup的值,涉及到引发的混乱现象

最近要实现从数据库读数据,该数据对应listview的item布局里面的RadioButton值,并且item布局里面还有EditText的控件. 如何将每一条对应的listview对应值获取出来呢? 实现原理是在作为ArrayAdapter的参数的数据源对象(如数组,集合),举例: ArrayList<MyClass> arrayList = new ArrayList<MyClass>(); ArrayAdapter arrayAdapter = new ArrayAdapte

使用 ViewPager 和 RadioGroup 封装的一个导航控件

import android.animation.ObjectAnimator; import android.content.Context; import android.graphics.drawable.Drawable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager;

RadioGroup&amp;RadioButton

RadioGroup提供多选一机制:属性orientation:“vertlcal” or “horizontal” @Override public void onCheckedChanged(RadioGroup group, int checkedid) { // TODO Auto-generated method stub switch(checkedid){ case R.id.radio0: Log.i("tag","nan"); break; cas