现在的位置: 首页 > 综合 > 正文

Android 动态创建布局,设置新增控件ID

2014年02月27日 ⁄ 综合 ⁄ 共 6439字 ⁄ 字号 评论关闭

最近做了一个调查问卷的东西,服务器返回的问题数,还有每个问题的选项个数都不固定,而且,有多选有单选,那数据结构整得我蛋疼。做好了,拿出来分享下

这个是联网的,所以代码中数据无法获取到。记录下来就是为了以后遇到类型的情况可以更熟练的搞定,因为一直对代码写布局不是很熟练

动态创建自己想要的布局流程我觉得应该这样

首先要在当前Context实例化一个需要的控件,找到要添加该控件的布局,addView该控件。如果要设置它的一些属性,可以通过LinearLayout.LayoutParams来做

一些其他的效果,就是靠这个控件的一些方法来搞了,什么设置文字,背景,焦点一些东西。之前我也做过类似的东西,就是搜索热词推荐,也是需要动态创建,

那时候迷迷糊糊的做完了,到现在都忘记了,这次巩固了一下。如果需要给控件加监听什么的,自己写个接口就好了,一定难不倒各位。

public class SurveyActivity extends Activity implements OnClickListener{
private List<SurveyBean> titleList;// 调查问卷问题集合
private List<SurveyOptionsBean> optionList;// 调查问卷选项集合
private List<RadioButton> radioButtonList;// 装载RadioButton的集合
private List<CheckBox> checkBoxList;// 装载CheckBox的集合
private List<String> surveyOptionIDList;// 装载surveyOptionIDList的集合
private List<String> surveyIDList;
private HashMap<String, String> surveyIDMap = new HashMap<String, String>();
private HashMap<String, List<SurveyOptionsBean>> hashMap;// 问题每个选项的信息集合
private SurveyEngine se;// 调查问卷请求
private StatusBean sBean;// 联网请求是否成功
private LinearLayout survey_layout;// 装载问题的layout(ScrollView包含的)
private int radioButtonSize = 0;// 计算布局中RadioButton的个数
private int checkBoxSize = 0;// 计算布局中CheckBox个数

private Button btnSub;// 提交选项

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.survey);
init();
loadModel();
initView();
}

private Handler handler = new Handler(){
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};

private void init(){
titleList = new ArrayList<SurveyBean>();
hashMap = new HashMap<String, List<SurveyOptionsBean>>();
radioButtonList = new ArrayList<RadioButton>();
checkBoxList = new ArrayList<CheckBox>();
surveyOptionIDList = new ArrayList<String>();
surveyIDList = new ArrayList<String>();
se = new SurveyEngine(this, handler);
survey_layout = (LinearLayout) findViewById(R.id.survey_layout);

btnSub = (Button) findViewById(R.id.btnSub);

}

private void initView(){
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int i=0; i<titleList.size(); i++){
// 获得moduleSurveyID,从hashMap取出该问题的选项集合
String moduleSurveyID = titleList.get(i).moduleSurveyID;
// 获得surveyType,判断是单选还是多选
String surveyType = titleList.get(i).surveyType;
// 该问题选项信息集合,装载SurveyOptionsBean类,包含surveyOptionID,optionTitle
optionList = hashMap.get(moduleSurveyID);
BWLog.d("问题选项信息集合----------->" + optionList.size());
// 调查问卷问题
TextView surveyTitle = new TextView(this);
surveyTitle.setLayoutParams(lp);
surveyTitle.setText(titleList.get(i).surveyTitle);
survey_layout.addView(surveyTitle);

// 1为单选2为多选
if (surveyType.equals("1")){
survey_layout.addView(getRadioGroup(optionList, moduleSurveyID));
radioButtonSize = radioButtonSize + optionList.size();
BWLog.d("radioButtonSize---------->" + radioButtonSize);
}else {
survey_layout.addView(getLinearLayout(optionList, moduleSurveyID));
checkBoxSize = checkBoxSize + optionList.size();
BWLog.d("checkBoxSize---------->" + checkBoxSize);
}
}
}

/*********************
* 生成一个用于单选的RadioGroup
* @param list
该问题选项信息集合
* @return
*/
private RadioGroup getRadioGroup(List<SurveyOptionsBean> list, String moduleSurveyID){
RadioGroup radioGroup = new RadioGroup(this);
// 设置RadioGroup为垂直
radioGroup.setOrientation(RadioGroup.VERTICAL);
for(int i=0; i<list.size(); i++){
RadioButton radio = new RadioButton(this);
radio.setText(list.get(i).optionTitle);
// radioGroup.setTag(list.get(i).surveyOptionID);

// 设置id,我是将封装数据中选项有一个唯一的id当做该控件id了
radio.setId(Integer.valueOf(list.get(i).surveyOptionID));
surveyIDMap.put(list.get(i).surveyOptionID, moduleSurveyID);
radioButtonList.add(radio);
radioGroup.addView(radio);
}
return radioGroup;
}

/*********************
* 生成一个用于多选的LinearLayout
* @param list
该问题选项信息集合
* @return
*/
private LinearLayout getLinearLayout(List<SurveyOptionsBean> list, String moduleSurveyID){
LinearLayout layout = new LinearLayout(this);
// 设置LinearLayout垂直
layout.setOrientation(LinearLayout.VERTICAL);
for(int i=0; i<list.size(); i++){
CheckBox cb = new CheckBox(this);
cb.setText(list.get(i).optionTitle);
// cb.setTag(list.get(i).surveyOptionID);

// 设置id,与RadioButton一样
cb.setId(Integer.valueOf(list.get(i).surveyOptionID));
surveyIDMap.put(list.get(i).surveyOptionID, moduleSurveyID);
checkBoxList.add(cb);
layout.addView(cb);
}
return layout;
}

/**********************************
* 获取界面数据
*/
private void loadModel(){
new GetSurveyListTask().execute();
}

/**
*调查问卷联网请求
*/
class GetSurveyListTask extends AsyncTask<Object, Object, Object>{

protected Object doInBackground(Object... params) {
try {
sBean = se.getSurveyRequest(titleList, hashMap);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

protected void onPreExecute() {
super.onPreExecute();
}

protected void onPostExecute(Object result) {
super.onPostExecute(result);
if (sBean.status.equals("-1")){
Toast.makeText(SurveyActivity.this, sBean.msg, Toast.LENGTH_SHORT).show();
}else {
initView();
}
}
}

/********************************
* 检查选项,获得对应选项id的数组
* @return
*/
private List<String> checkOptions(List<String> surveyIDList, List<String> list){
if (radioButtonList != null){
for(int i=0; i<radioButtonList.size(); i++){
boolean isSelected = radioButtonList.get(i).isChecked();
if (isSelected == true){
list.add(String.valueOf(radioButtonList.get(i).getId()));
surveyIDList.add(surveyIDMap.get(String.valueOf(radioButtonList.get(i).getId())));
}
}
}
if (checkBoxList != null){
for(int i=0; i<checkBoxList.size(); i++){
boolean isSelected = checkBoxList.get(i).isChecked();
if (isSelected == true){
list.add(String.valueOf(checkBoxList.get(i).getId()));
surveyIDList.add(surveyIDMap.get(String.valueOf(checkBoxList.get(i).getId())));
}
}
}

return list;
}

/**
*提交调查问卷全部短选、多选结果
*/
class SetSurveyOptionsTask extends AsyncTask<Object, Object, Object>{
StatusBean statusBean;

protected Object doInBackground(Object... params) {
try {
statusBean = se.SurveyOptionsRequest(surveyIDList, surveyOptionIDList);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

protected void onPreExecute() {
super.onPreExecute();
dialog = DialogUitl.createProgressDialog(SurveyActivity.this);
dialog.show();
checkOptions(surveyIDList, surveyOptionIDList);
}

protected void onPostExecute(Object result) {
super.onPostExecute(result);
dialog.dismiss();
if (statusBean.status.equals("-1")){
Toast.makeText(SurveyActivity.this, statusBean.msg, Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(SurveyActivity.this, statusBean.msg, Toast.LENGTH_SHORT).show();
}
}
}

public void onClick(View v) {
switch(v.getId()){
case R.id.btnSub:
if(surveyOptionIDList == null){
Toast.makeText(this, "未选中", Toast.LENGTH_SHORT).show();
}else{
new SetSurveyOptionsTask().execute();
}
break;
case R.id.btnBack:
finish();
break;
}
}
}

提交选项时,我需要相应RadioButton  /  CheckBox的信息,还有它们所对应的问题ID,所以就用了一个HashMap来装载,因为每个选项的ID信息是唯一的,那么就通过它来找到对应的问题ID,因为比较菜,想不出来有什么好的方法解决。我把动态创建的控件都放在了一个ScrollView中,效果是酱婶儿的

【上篇】
【下篇】

抱歉!评论已关闭.