替换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wj993257104/article/details/52064452
布局
<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"
    tools:context=".MainActivity"
    android:orientation="vertical" >
     
   <FrameLayout 
       android:layout_weight="5"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:id="@+id/fl"
       />
  <RadioGroup 
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="0.5"
      android:orientation="horizontal"
      android:id="@+id/rg"
      android:background="#ff0"
      >
      <RadioButton 
          android:layout_width="0dp"
          android:layout_weight="1"
          android:layout_height="match_parent"
          android:text="订阅"
          
          android:gravity="center"
          android:button="@null"
          android:id="@+id/rb1"/>
      <RadioButton 
          android:gravity="center"
          android:button="@null"
          android:layout_width="0dp"
          android:layout_weight="1"
          android:layout_height="match_parent"
          android:text="热点"
          android:id="@+id/rb2"/>
      <RadioButton
          android:gravity="center" 
          android:button="@null"
          android:layout_width="0dp"
          android:layout_weight="1"
          android:layout_height="match_parent"
          android:text="学习"
          android:id="@+id/rb3"/>
     <RadioButton
          android:gravity="center" 
          android:button="@null"
          android:layout_width="0dp"
          android:layout_weight="1"
          android:layout_height="match_parent"
          android:text="我的"
          android:id="@+id/rb4"/>
    
      </RadioGroup>
    
</LinearLayout>


代码
package com.bwei.zakernews;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.bwei.fragment.Fragmenme;
import com.bwei.fragment.Fragmentdingyue;
import com.bwei.fragment.Fragmentredian;
import com.bwei.fragment.Fragmentwanle;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

public class MainActivity extends FragmentActivity implements OnClickListener{
	//全局变量
	private FrameLayout fl;
	private RadioGroup rg;
	private RadioButton rb1;
	private RadioButton rb2;
	private RadioButton rb3;
	private FragmentManager manager;
	private Fragmentdingyue f1;
	private Fragmentredian f2;
	private Fragmentwanle f3;
	private RadioButton rb4;
	private Fragmenme f4;
	private UMShareAPI mShareAPI;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//找控件
		fl = (FrameLayout) findViewById(R.id.fl);
		rg = (RadioGroup) findViewById(R.id.rg);
		rb1 = (RadioButton) findViewById(R.id.rb1);
		rb2 = (RadioButton) findViewById(R.id.rb2);
		rb3 = (RadioButton) findViewById(R.id.rb3);
		rb4 = (RadioButton) findViewById(R.id.rb4);
	    rb1.setChecked(true);
	    rb1.setTextColor(Color.RED);
	    rb1.setOnClickListener(this);
	    rb2.setOnClickListener(this);
	    rb3.setOnClickListener(this);
	    rb4.setOnClickListener(this);
	    mShareAPI = UMShareAPI.get(this);
	   manager = getSupportFragmentManager();
	      FragmentTransaction transaction = manager.beginTransaction();
	      f1 = new Fragmentdingyue();
	      f2 = new Fragmentredian();
	      f3 = new Fragmentwanle();
	      f4 = new Fragmenme();
	      transaction.add(R.id.fl,f1, "f1");
	      transaction.commit();  
	}
	
	

	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.rb1:
			    rb1.setTextColor(Color.RED);
			    rb2.setTextColor(Color.BLACK);
			    rb3.setTextColor(Color.BLACK);
			    rb4.setTextColor(Color.BLACK);
			    swithTag("f1");
			break;
      case R.id.rb2:
    	    rb2.setTextColor(Color.RED);
		    rb1.setTextColor(Color.BLACK);
		    rb3.setTextColor(Color.BLACK);
		    rb4.setTextColor(Color.BLACK);
		    swithTag("f2");
			break;
      case R.id.rb3:
    	    rb3.setTextColor(Color.RED);
		    rb2.setTextColor(Color.BLACK);
		    rb1.setTextColor(Color.BLACK);
		    rb4.setTextColor(Color.BLACK);
		    swithTag("f3");
           break;
      case R.id.rb4:
  	    rb4.setTextColor(Color.RED);
		    rb2.setTextColor(Color.BLACK);
		    rb1.setTextColor(Color.BLACK);
		    rb3.setTextColor(Color.BLACK);
		    swithTag("f4");
         break;
		}
	}
	
	public void login()
	{
		SHARE_MEDIA platform = SHARE_MEDIA.QQ;
		mShareAPI.doOauthVerify(this, platform,
				umAuthListener);
	}
	private void swithTag(String Tag) {
		
		if("f1".equals(Tag)){
		FragmentTransaction transaction = manager.beginTransaction();
		transaction.replace(R.id.fl,f1,"f1");
		transaction.commit();
		}else if("f2".equals(Tag)){
			FragmentTransaction transaction = manager.beginTransaction();
			transaction.replace(R.id.fl,f2,"f2");
			transaction.commit();
			}else if("f3".equals(Tag)){
				FragmentTransaction transaction = manager.beginTransaction();
				transaction.replace(R.id.fl,f3,"f3");
				transaction.commit();	
				}
			else if("f4".equals(Tag)){
				FragmentTransaction transaction = manager.beginTransaction();
				transaction.replace(R.id.fl,f4,"f4");
				transaction.commit();	
				}
		
	}
】

}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页