Radiobutton
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Radiobutton相关的知识,希望对你有一定的参考价值。
package com.example.yuekao3;
import java.util.ArrayList;
import java.util.List;
import com.baidu.farment.F1;
import com.baidu.farment.Fra;
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.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnClickListener {
private LinearLayout lin;
int width;
private ViewPager vp;
private RadioButton r1;
private RadioButton r2;
private RadioButton r3;
private RadioButton r4;
private List<TextView> text_lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
r1 = (RadioButton) findViewById(R.id.r1);
r2 = (RadioButton) findViewById(R.id.r2);
r3 = (RadioButton) findViewById(R.id.r3);
r4 = (RadioButton) findViewById(R.id.r4);
int width = getWindowManager().getDefaultDisplay().getWidth();
r1.setOnClickListener(this);
r2.setOnClickListener(this);
r3.setOnClickListener(this);
r4.setOnClickListener(this);
lin = (LinearLayout) findViewById(R.id.lin);
text_lv = new ArrayList<TextView>();
for (int i = 0; i < 4; i++) {
TextView text = new TextView(this);
LinearLayout.LayoutParams params = new LayoutParams(width / 4,
LayoutParams.WRAP_CONTENT);
text.setBackgroundColor(Color.GREEN);
text.setVisibility(View.INVISIBLE);
text_lv.add(text);
lin.addView(text, i,params);
}
vp = (ViewPager) findViewById(R.id.viewpager);
getdata();
FragmentManager fm = getSupportFragmentManager();
Fra f = new Fra(fm, getdata());
vp.setAdapter(f);
//vp.setCurrentItem(0);
//r1.setTextColor(Color.GREEN);
//text_lv.get(0).setVisibility(View.VISIBLE);
vp.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);
text_lv.get(0).setVisibility(View.VISIBLE);
text_lv.get(1).setVisibility(View.INVISIBLE);
text_lv.get(2).setVisibility(View.INVISIBLE);
text_lv.get(3).setVisibility(View.INVISIBLE);
break;
case 1:
r2.setTextColor(Color.GREEN);
r1.setTextColor(Color.BLACK);
r3.setTextColor(Color.BLACK);
r4.setTextColor(Color.BLACK);
text_lv.get(1).setVisibility(View.VISIBLE);
text_lv.get(0).setVisibility(View.INVISIBLE);
text_lv.get(2).setVisibility(View.INVISIBLE);
text_lv.get(3).setVisibility(View.INVISIBLE);
break;
case 2:
r3.setTextColor(Color.GREEN);
r2.setTextColor(Color.BLACK);
r1.setTextColor(Color.BLACK);
r4.setTextColor(Color.BLACK);
text_lv.get(2).setVisibility(View.VISIBLE);
text_lv.get(1).setVisibility(View.INVISIBLE);
text_lv.get(0).setVisibility(View.INVISIBLE);
text_lv.get(3).setVisibility(View.INVISIBLE);
break;
case 3:
r4.setTextColor(Color.GREEN);
r3.setTextColor(Color.BLACK);
r2.setTextColor(Color.BLACK);
r1.setTextColor(Color.BLACK);
text_lv.get(3).setVisibility(View.VISIBLE);
text_lv.get(2).setVisibility(View.INVISIBLE);
text_lv.get(1).setVisibility(View.INVISIBLE);
text_lv.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 List<Fragment> getdata() {
// TODO Auto-generated method stub
List<Fragment> list = new ArrayList<Fragment>();
for (int i = 0; i < 4; i++) {
list.add(new F1());
}
return list;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.r1:
vp.setCurrentItem(0);
break;
case R.id.r2:
vp.setCurrentItem(1);
break;
case R.id.r3:
vp.setCurrentItem(2);
break;
case R.id.r4:
vp.setCurrentItem(3);
break;
}
}
}
以上是关于Radiobutton的主要内容,如果未能解决你的问题,请参考以下文章