您的位置:首页 > 移动开发 > Android开发

Android中RadioGroup和RadioButton布局实例

2010-01-11 16:09 495 查看
package net.set;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class netset extends Activity {

/** Called when the activity is first created. */
private LinearLayout l;

TextView tv1;
EditText et1;

TextView tv2;
EditText et2;

TextView tv3;
EditText et3;

TextView tv4;
EditText et4;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);

l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
// l.setBackgroundColor(android.graphics.Color.WHITE);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT
);
l.setLayoutParams(params);

TextView tv_title=new TextView(this);
tv_title.setBackgroundColor(android.R.color.white);
tv_title.setText("网络设置:");
l.addView(tv_title);

RelativeLayout r = new RelativeLayout(this);

RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
params2.leftMargin=300;
params2.topMargin=40;
l.setLayoutParams(params2);
TextView tv_ban=new TextView(this);
tv_ban.setBackgroundColor(android.R.color.white);
tv_ban.setText("I版本:");
r.addView(tv_ban,params2);

//单选按钮组1:
RadioGroup rg2=new RadioGroup(this);
RadioGroup.LayoutParams lp=new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT
);
rg2.setOrientation(LinearLayout.HORIZONTAL);

RelativeLayout.LayoutParams p_rb_rg2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
p_rb_rg2.leftMargin=350;
p_rb_rg2.topMargin=70;

r.addView(rg2, p_rb_rg2);

RadioButton rb1=new RadioButton(this);
rb1.setText("I1");
rb1.setId(201);
RadioGroup.LayoutParams lp_rb1_rg2=new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT
);

rg2.addView(rb1, 0, lp_rb1_rg2);

RadioButton rb2=new RadioButton(this);
rb2.setText("I2");
rb2.setId(202);
RadioGroup.LayoutParams lp_rb2_rg2=new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT
);

rg2.addView(rb2, 1, lp_rb2_rg2);

RadioButton rb3=new RadioButton(this);
rb3.setText("I3");
rb3.setId(203);
RadioGroup.LayoutParams lp_rb3_rg2=new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT
);

rg2.addView(rb3, 2, lp_rb3_rg2);

RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
params1.leftMargin=300;
params1.topMargin=110;
l.setLayoutParams(params1);

TextView tv1_net=new TextView(this);
tv1_net.setBackgroundColor(android.R.color.white);
tv1_net.setText("网络连接方式:");
r.addView(tv1_net,params1);

//单选按钮组2:
RadioGroup rg1=new RadioGroup(this);
RadioGroup.LayoutParams lp_1=new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT
);
rg1.setOrientation(LinearLayout.HORIZONTAL);

RelativeLayout.LayoutParams p_rb_rg1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
p_rb_rg1.leftMargin=350;
p_rb_rg1.topMargin=140;

r.addView(rg1, p_rb_rg1);

RadioButton rb1_rg1=new RadioButton(this);

rb1_rg1.setText("静态");
rb1_rg1.setId(101);
RadioGroup.LayoutParams lp_rb1_rg1=new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT
);

rg1.addView(rb1_rg1, 0, lp_rb1_rg1);

RadioButton rb2_rg1=new RadioButton(this);
rb2_rg1.setText("动态");
rb2_rg1.setId(102);
RadioGroup.LayoutParams lp_rb2_rg1=new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT
);

rg1.addView(rb2_rg1, 1, lp_rb2_rg1);

RadioButton rb3_rg1=new RadioButton(this);
rb3_rg1.setText("PPOE");
rb3_rg1.setId(103);
RadioGroup.LayoutParams lp_rb3_rg1=new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT
);

rg1.addView(rb3_rg1, 2, lp_rb3_rg1);

tv1=new TextView(this);
et1=new EditText(this);
tv2=new TextView(this);
et2=new EditText(this);
tv3=new TextView(this);
et3=new EditText(this);
tv4=new TextView(this);
et4=new EditText(this);

//TableView

rg1.setOnCheckedChangeListener(
new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(
RadioGroup rg1,
int checkedId) {

checkedId=rg1.getCheckedRadioButtonId();
if(checkedId==101){
System.out.println(checkedId);
l.removeView(tv1);
l.removeView(et1);
l.removeView(tv2);
l.removeView(et2);
l.removeView(tv3);
l.removeView(et3);
l.removeView(tv4);
l.removeView(et4);
showBox1();
l.addView(tv1);
l.addView(et1);
l.addView(tv2);
l.addView(et2);
l.addView(tv3);
l.addView(et3);
l.addView(tv4);
l.addView(et4);
}
if(checkedId==102){
System.out.println(checkedId);
l.removeView(tv1);
l.removeView(et1);
l.removeView(tv2);
l.removeView(et2);
l.removeView(tv3);
l.removeView(et3);
l.removeView(tv4);
l.removeView(et4);
showBox2();
l.addView(tv1);
l.addView(et1);
l.addView(tv2);
l.addView(et2);
l.addView(tv3);
l.addView(et3);
l.addView(tv4);
l.addView(et4);
}
if(checkedId==103){
System.out.println(checkedId);
l.removeView(tv1);
l.removeView(et1);
l.removeView(tv2);
l.removeView(et2);
l.removeView(tv3);
l.removeView(et3);
l.removeView(tv4);
l.removeView(et4);
showBox3();
l.addView(tv1);
l.addView(et1);
l.addView(tv2);
l.addView(et2);

}
}
});

l.addView(r,params1);

setContentView(l);

}

public void showBox1(){

tv1.setText("IP:");

et1.setText("");

tv2.setText("网关:");

et2.setText("");

tv3.setText("掩码:");

et3.setText("");

tv4.setText("DNS:");

et4.setText("");

}
public void showBox2(){

}
public void showBox3(){
tv1.setText("用户名:");

et1.setText("");

tv2.setText("密码:");

et2.setText("");

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: