安卓之问卷调查

来源:互联网 发布:ipad1越狱安装软件 编辑:程序博客网 时间:2024/04/29 19:11

       项目中,客户要求增加问卷调查功能,类似PC端的提问回答问题的提示框。因为需要服务器做交互,为了能使用,自己先定义了一个JsonResult类,代表服务器返回的参数。好了,不废话了直接上代码

    下载地址http://download.csdn.net/detail/u012303938/8389877

    JsonResult类

package com.example.questionnaire;public class JsonResult {private String result="{\"cid\":\"101\",\"mobile\":\"15253144620\",\"flag\":\"success\",\"cause\":\"\",\"ename\":\"张三\",\"employno\":\"002\",\"dept\":\"市场部\",\"rowcount\":\"5\",\"currentpage\":\"1\" ,\"vote\":[{\"id\":\"132\",\"Title\":\"对公司的意见\",\"stype\":\"类型1\",\"SelCount\":\"0\",\"Sel1\":\"\",\"Sel2\":\"\",\"Sel3\":\"\",\"Sel4\":\"\",\"Sel5\":\"\",\"Sel6\":\"\",\"Sel7\":\"\",\"Sel8\":\"\",\"Sel9\":\"\",\"Sel10\":\"\",\"ctype\":\"3\"},{\"id\":\"131\",\"Title\":\"今天看见雪了吗?\",\"stype\":\"类型2\",\"SelCount\":\"2\",\"Sel1\":\"是\",\"Sel2\":\"否\",\"Sel3\":\"\",\"Sel4\":\"\",\"Sel5\":\"\",\"Sel6\":\"\",\"Sel7\":\"\",\"Sel8\":\"\",\"Sel9\":\"\",\"Sel10\":\"\",\"ctype\":\"4\"},{\"id\":\"130\",\"Title\":\"你今天中午吃什么?\",\"stype\":\"类型1\",\"SelCount\":\"4\",\"Sel1\":\"米饭\",\"Sel2\":\"鸡蛋\",\"Sel3\":\"鸡肉\",\"Sel4\":\"猪肉\",\"Sel5\":\"\",\"Sel6\":\"\",\"Sel7\":\"\",\"Sel8\":\"\",\"Sel9\":\"\",\"Sel10\":\"\",\"ctype\":\"5\"},{\"id\":\"129\",\"Title\":\"哈哈\",\"stype\":\"类型2\",\"SelCount\":\"4\",\"Sel1\":\"第一\",\"Sel2\":\"第二\",\"Sel3\":\"第三\",\"Sel4\":\"第四\",\"Sel5\":\"\",\"Sel6\":\"\",\"Sel7\":\"\",\"Sel8\":\"\",\"Sel9\":\"\",\"Sel10\":\"\",\"ctype\":\"2\"},{\"id\":\"128\",\"Title\":\"标题\",\"stype\":\"类型1\",\"SelCount\":\"3\",\"Sel1\":\"第一\",\"Sel2\":\"第二\",\"Sel3\":\"第三\",\"Sel4\":\"\",\"Sel5\":\"\",\"Sel6\":\"\",\"Sel7\":\"\",\"Sel8\":\"\",\"Sel9\":\"\",\"Sel10\":\"\",\"ctype\":\"1\"}]}";public String getResult() {return result;}public void setResult(String result) {this.result = result;}    }
主类:

package com.example.questionnaire;import java.util.ArrayList;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.os.Bundle;import android.app.Activity;import android.app.ProgressDialog;import android.util.Log;import android.view.Gravity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.webkit.WebView;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.LinearLayout;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.ScrollView;import android.widget.TextView;import android.widget.Toast;import android.widget.LinearLayout.LayoutParams;public class MainActivity extends Activity {private Button BtnLeft, btn_right;private List<LinkedHashMap<String, Object>> list;private int pos=0;private RadioButton rbtn[]=null;private EditText checkBox;private CheckBox checkBo[];private TextView tv_sum;private ScrollView scroll;private boolean bll=true;private boolean boo=true;private ArrayList<Map<String, String>> zhi=new ArrayList<Map<String,String>>();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        this.requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_main);       //实例化        findView();        //去加载数据        loadData();        //监听        addListener();    }    private void addListener() {// TODO Auto-generated method stub    BtnLeft.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubfinish();}});}private void findView() {// TODO Auto-generated method stubtv_sum=(TextView) findViewById(R.id.tv_sum);    BtnLeft=(Button) findViewById(R.id.btn_cancle);btn_right=(Button)findViewById(R.id.btn_right);scroll=(ScrollView) findViewById(R.id.scroll);btn_right.setVisibility(View.GONE);}private void loadData() {// TODO Auto-generated method stub    //用JSON去解析得到的数据    JsonResult result=new JsonResult();    try {JSONObject jsonObject = new JSONObject(result.getResult().toString());if ("success".equals(jsonObject.getString("flag"))) {Toast.makeText(MainActivity.this, "asd", 1).show();JSONArray array = jsonObject.getJSONArray("vote");list = new ArrayList<LinkedHashMap<String, Object>>();int len = array.length();for (int i = 0; i < len; i++) {LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();map.put("Title",array.getJSONObject(i).getString("Title"));map.put("id", array.getJSONObject(i).getString("id"));map.put("SelCount", array.getJSONObject(i).getString("SelCount"));map.put("ctype",array.getJSONObject(i).getString("ctype"));String sel = array.getJSONObject(i).getString("SelCount");int l = Integer.valueOf(sel);for (int j = 0; j < l; j++) {map.put("sel" + j,array.getJSONObject(i).getString("Sel" + String.valueOf(j + 1)));}//用list封装进去自己解析好的数据list.add(map);Log.i("values", "len" + list);}//在主布局添加控件addViews();  } else {Toast.makeText(MainActivity.this, "暂无数据",Toast.LENGTH_SHORT).show();}    } catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private void addViews() {// TODO Auto-generated method stubtv_sum.setText(pos+"/"+list.size());if(pos<list.size()&&pos>=0){String cty = (String)list.get(pos).get("ctype");Log.i("values", "ctype"+cty);if(cty.equals("1")){String s = (String) list.get(pos).get("SelCount");RadioGroup radioGroup = new RadioGroup(MainActivity.this);LinearLayout.LayoutParams raParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);radioGroup.setLayoutParams(raParams);radioGroup.setOrientation(LinearLayout.VERTICAL);int l = Integer.valueOf(s);rbtn=new RadioButton[l];Log.i("values", "ctype"+l);for (int j = 0; j < l; j++) {LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);rbtn[j]= new RadioButton(MainActivity.this);rbtn[j].setLayoutParams(Params);String str = (String) list.get(pos).get("sel" + String.valueOf(j));rbtn[j].setText(str);radioGroup.addView(rbtn[j]);}LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);btnParams.weight=1;LinearLayout danlayout=new LinearLayout(MainActivity.this);danlayout.setLayoutParams(titleParams);danlayout.setOrientation(LinearLayout.VERTICAL);TextView titles = new TextView(MainActivity.this);titles.setLayoutParams(titleParams);titles.setText("单选题 ");titles.setGravity(Gravity.CENTER);titles.setTextSize(20f);TextView title = new TextView(MainActivity.this);title.setLayoutParams(titleParams);title.setText("标题: "+ (String) list.get(pos).get("Title"));title.setPadding(20, 0, 0, 0);LinearLayout ln=new LinearLayout(MainActivity.this);ln.setLayoutParams(titleParams);ln.setOrientation(LinearLayout.HORIZONTAL);Button btns=new Button(MainActivity.this);final Button btnx=new Button(MainActivity.this);btnx.setLayoutParams(btnParams);btns.setLayoutParams(btnParams);if(pos==0){btns.setEnabled(false);}else{btns.setEnabled(true);}btns.setText("上一题");btnx.setText("下一题");if(pos==list.size()-1){btnx.setText("完成");}btnx.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubfor(int i=0;i<rbtn.length;i++){if(rbtn[i].isChecked()){Map<String, String> map=new HashMap<String, String>();map.put("id", (String)list.get(pos).get("id"));map.put("content", String.valueOf(i+1));map.put("type", "1");map.put("qita", "");zhi.add(map);bll=false;break;}}if(bll){Map<String, String> map=new HashMap<String, String>();map.put("id", (String)list.get(pos).get("id"));map.put("content", "");map.put("type", "1");map.put("qita", "");zhi.add(map);}pos++;Log.i("values", "pos:"+(pos));Log.i("values", "zhi:"+zhi.size());if(pos<list.size()){scroll.removeAllViews();//layout.removeViewAt(1);}else{btnx.setEnabled(false);btn_right.setVisibility(View.VISIBLE);}addViews();}});btns.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubzhi.remove(pos-1);Log.i("values", "pos:"+(pos));Log.i("values", "zhi:"+zhi.size());pos--;scroll.removeAllViews();//layout.removeViewAt(1);addViews();btn_right.setVisibility(View.GONE);}});danlayout.addView(titles);danlayout.addView(title);danlayout.addView(radioGroup);ln.addView(btns);ln.addView(btnx);danlayout.addView(ln);scroll.addView(danlayout);//layout.addView(scroll);}else if(cty.equals("2")){String s = (String) list.get(pos).get("SelCount");int l = Integer.valueOf(s);LinearLayout.LayoutParams raParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);LinearLayout radlayout = new LinearLayout(MainActivity.this);LinearLayout alllayout = new LinearLayout(MainActivity.this);alllayout.setLayoutParams(raParams);alllayout.setOrientation(LinearLayout.VERTICAL);radlayout.setLayoutParams(raParams);radlayout.setOrientation(LinearLayout.VERTICAL);checkBo=new CheckBox[l];for (int j = 0; j < l; j++) {LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);checkBo[j] = new CheckBox(MainActivity.this);checkBo[j].setLayoutParams(Params);String str = (String) list.get(pos).get("sel" + String.valueOf(j));Log.i("values", "msg" + str);checkBo[j].setText(str);radlayout.addView(checkBo[j]);}LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);btnParams.weight=1;LinearLayout danlayout=new LinearLayout(MainActivity.this);danlayout.setLayoutParams(titleParams);danlayout.setOrientation(LinearLayout.VERTICAL);TextView title = new TextView(MainActivity.this);title.setLayoutParams(titleParams);title.setText("标题: "+ (String) list.get(pos).get("Title"));title.setPadding(20, 0, 0, 0);TextView titles = new TextView(MainActivity.this);titles.setLayoutParams(titleParams);titles.setText("多选题 ");titles.setGravity(Gravity.CENTER);titles.setTextSize(20f);LinearLayout ln=new LinearLayout(MainActivity.this);ln.setLayoutParams(titleParams);ln.setOrientation(LinearLayout.HORIZONTAL);Button btns=new Button(MainActivity.this);final Button btnx=new Button(MainActivity.this);btnx.setLayoutParams(btnParams);btns.setLayoutParams(btnParams);if(pos==0){btns.setEnabled(false);}else{btns.setEnabled(true);}btns.setText("上一题");btnx.setText("下一题");if(pos==list.size()-1){btnx.setText("完成");}btnx.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubMap<String, String> map=new HashMap<String, String>();String str="";map.put("id", (String)list.get(pos).get("id"));for(int i=0;i<checkBo.length;i++){if(checkBo[i].isChecked()){str+=String.valueOf(i+1)+",";}}if(!str.equals("")){str=str.substring(0, str.length()-1);}map.put("content", str);map.put("type", "2");map.put("qita", "");zhi.add(map);pos++;Log.i("values", "pos:"+(pos));Log.i("values", "zhi:"+zhi.size());if(pos<list.size()){scroll.removeAllViews();//layout.removeViewAt(1);}else{btnx.setEnabled(false);btn_right.setVisibility(View.VISIBLE);}addViews();}});btns.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubzhi.remove(pos-1);pos--;Log.i("values", "pos:"+(pos));Log.i("values", "zhi:"+zhi.size());scroll.removeAllViews();//layout.removeViewAt(1);addViews();btn_right.setVisibility(View.GONE);}});danlayout.addView(titles);danlayout.addView(title);danlayout.addView(radlayout);ln.addView(btns);ln.addView(btnx);danlayout.addView(ln);scroll.addView(danlayout);//layout.addView(scroll);}else if(cty.equals("3")){LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);btnParams.weight=1;LinearLayout alllayout = new LinearLayout(MainActivity.this);alllayout.setLayoutParams(titleParams);alllayout.setOrientation(LinearLayout.VERTICAL);TextView titles = new TextView(MainActivity.this);titles.setLayoutParams(titleParams);titles.setText("开放题 ");titles.setGravity(Gravity.CENTER);titles.setTextSize(20f);LinearLayout danlayout=new LinearLayout(MainActivity.this);danlayout.setLayoutParams(titleParams);danlayout.setOrientation(LinearLayout.VERTICAL);TextView title = new TextView(MainActivity.this);title.setLayoutParams(titleParams);title.setText("标题: "+ (String) list.get(pos).get("Title"));title.setPadding(20, 0, 0, 0);LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);checkBox= new EditText(MainActivity.this);checkBox.setLayoutParams(Params);LinearLayout ln=new LinearLayout(MainActivity.this);ln.setLayoutParams(titleParams);ln.setOrientation(LinearLayout.HORIZONTAL);Button btns=new Button(MainActivity.this);final Button btnx=new Button(MainActivity.this);btnx.setLayoutParams(btnParams);btns.setLayoutParams(btnParams);if(pos==0){btns.setEnabled(false);}else{btns.setEnabled(true);}if(pos==list.size()){btnx.setEnabled(false);btnx.setText("题已完成");}else{btnx.setEnabled(true);}btns.setText("上一题");btnx.setText("下一题");if(pos==list.size()-1){btnx.setText("完成");}btnx.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubMap<String, String> map=new HashMap<String, String>();map.put("id", (String)list.get(pos).get("id"));map.put("content", checkBox.getText().toString());map.put("type", "3");map.put("qita", "");pos++;zhi.add(map);Log.i("values", "pos:"+(pos));Log.i("values", "zhi:"+zhi.size());if(pos<list.size()){scroll.removeAllViews();}else {btnx.setEnabled(false);btn_right.setVisibility(View.VISIBLE);}addViews();}});btns.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubzhi.remove(pos-1);pos--;Log.i("values", "pos:"+(pos));Log.i("values", "zhi:"+zhi.size());scroll.removeAllViews();//layout.removeViewAt(1);addViews();btn_right.setVisibility(View.GONE);}});alllayout.addView(titles);alllayout.addView(title);alllayout.addView(checkBox);ln.addView(btns);ln.addView(btnx);alllayout.addView(ln);danlayout.addView(alllayout);scroll.addView(danlayout);//layout.addView(scroll);}else if(cty.equals("5")){String s = (String) list.get(pos).get("SelCount");int l = Integer.valueOf(s);LinearLayout.LayoutParams raParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);LinearLayout radlayout = new LinearLayout(MainActivity.this);LinearLayout alllayout = new LinearLayout(MainActivity.this);alllayout.setLayoutParams(raParams);alllayout.setOrientation(LinearLayout.VERTICAL);radlayout.setLayoutParams(raParams);radlayout.setOrientation(LinearLayout.VERTICAL);checkBo=new CheckBox[l];LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);btnParams.weight=1;LinearLayout danlayout=new LinearLayout(MainActivity.this);danlayout.setLayoutParams(titleParams);danlayout.setOrientation(LinearLayout.VERTICAL);Button btn=new Button(MainActivity.this);btn.setLayoutParams(btnParams);btn.setText("其他");final EditText et=new EditText(MainActivity.this);et.setLayoutParams(titleParams);et.setVisibility(View.GONE);for (int j = 0; j < l; j++) {LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);checkBo[j] = new CheckBox(MainActivity.this);checkBo[j].setLayoutParams(Params);String str = (String) list.get(pos).get("sel" + String.valueOf(j));Log.i("values", "msg" + str);checkBo[j].setText(str);radlayout.addView(checkBo[j]);}TextView title = new TextView(MainActivity.this);title.setLayoutParams(titleParams);title.setText("标题: "+ (String) list.get(pos).get("Title"));title.setPadding(20, 0, 0, 0);TextView titles = new TextView(MainActivity.this);titles.setLayoutParams(titleParams);titles.setText("多选综合题 ");titles.setGravity(Gravity.CENTER);titles.setTextSize(20f);LinearLayout ln=new LinearLayout(MainActivity.this);ln.setLayoutParams(titleParams);ln.setOrientation(LinearLayout.HORIZONTAL);Button btns=new Button(MainActivity.this);final Button btnx=new Button(MainActivity.this);btnx.setLayoutParams(btnParams);btns.setLayoutParams(btnParams);if(pos==0){btns.setEnabled(false);}else{btns.setEnabled(true);}btns.setText("上一题");btnx.setText("下一题");if(pos==list.size()-1){btnx.setText("完成");}btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubet.setVisibility(View.VISIBLE);}});btnx.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubMap<String, String> map=new HashMap<String, String>();String str="";map.put("id", (String)list.get(pos).get("id"));for(int i=0;i<checkBo.length;i++){if(checkBo[i].isChecked()){str+=String.valueOf(i+1)+",";}}if(!str.equals("")){str=str.substring(0, str.length()-1);}String ets="";if(et.getVisibility()==View.VISIBLE){ets=et.getText().toString().trim();}map.put("qita", ets);map.put("content", str);map.put("type", "5");zhi.add(map);pos++;Log.i("values", "pos:"+(pos));Log.i("values", "zhi:"+zhi.size());if(pos<list.size()){scroll.removeAllViews();//layout.removeViewAt(1);}else{btnx.setEnabled(false);btn_right.setVisibility(View.VISIBLE);}addViews();}});btns.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubLog.i("values", "pos:"+pos);zhi.remove(pos-1);pos--;Log.i("values", "pos:"+(pos));Log.i("values", "zhi:"+zhi.size());scroll.removeAllViews();//layout.removeViewAt(1);addViews();btn_right.setVisibility(View.GONE);}});danlayout.addView(titles);danlayout.addView(title);danlayout.addView(radlayout);danlayout.addView(btn);danlayout.addView(et);ln.addView(btns);ln.addView(btnx);danlayout.addView(ln);scroll.addView(danlayout);}else if(cty.equals("4")){String s = (String) list.get(pos).get("SelCount");RadioGroup radioGroup = new RadioGroup(MainActivity.this);LinearLayout.LayoutParams raParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);radioGroup.setLayoutParams(raParams);radioGroup.setOrientation(LinearLayout.VERTICAL);final int l = Integer.valueOf(s);rbtn=new RadioButton[l];Log.i("values", "ctype"+l);Button btn=new Button(MainActivity.this);btn.setLayoutParams(btnParams);btn.setText("其他");final EditText et=new EditText(MainActivity.this);et.setLayoutParams(titleParams);et.setVisibility(View.GONE);for (int j = 0; j < l; j++) {LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);rbtn[j]= new RadioButton(MainActivity.this);rbtn[j].setLayoutParams(Params);String str = (String) list.get(pos).get("sel" + String.valueOf(j));rbtn[j].setText(str);rbtn[j].setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubet.setVisibility(View.GONE);}});radioGroup.addView(rbtn[j]);}btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubet.setVisibility(View.VISIBLE);for(int n=0;n<l;n++){rbtn[n].setChecked(false);}}});btnParams.weight=1;LinearLayout danlayout=new LinearLayout(MainActivity.this);danlayout.setLayoutParams(titleParams);danlayout.setOrientation(LinearLayout.VERTICAL);TextView titles = new TextView(MainActivity.this);titles.setLayoutParams(titleParams);titles.setText("单选综合题 ");titles.setGravity(Gravity.CENTER);titles.setTextSize(20f);TextView title = new TextView(MainActivity.this);title.setLayoutParams(titleParams);title.setText("标题: "+ (String) list.get(pos).get("Title"));title.setPadding(20, 0, 0, 0);LinearLayout ln=new LinearLayout(MainActivity.this);ln.setLayoutParams(titleParams);ln.setOrientation(LinearLayout.HORIZONTAL);Button btns=new Button(MainActivity.this);final Button btnx=new Button(MainActivity.this);btnx.setLayoutParams(btnParams);btns.setLayoutParams(btnParams);if(pos==0){btns.setEnabled(false);}else{btns.setEnabled(true);}btns.setText("上一题");btnx.setText("下一题");if(pos==list.size()-1){btnx.setText("完成");}btnx.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubif(et.getVisibility()==View.VISIBLE){Map<String, String> map=new HashMap<String, String>();map.put("id", (String)list.get(pos).get("id"));map.put("qita", et.getText().toString());map.put("type", "4");map.put("content", "");zhi.add(map);boo=false;}for(int i=0;i<rbtn.length;i++){if(rbtn[i].isChecked()){Map<String, String> map=new HashMap<String, String>();map.put("id", (String)list.get(pos).get("id"));map.put("content", String.valueOf(i+1));map.put("type", "4");map.put("qita", "");zhi.add(map);Log.i("values", "单选被执行了");boo=false;break;}}if(boo){Map<String, String> map=new HashMap<String, String>();map.put("id", (String)list.get(pos).get("id"));map.put("qita", "");map.put("type", "4");map.put("content", "");zhi.add(map);}                       pos++;Log.i("values", "pos:"+(pos));Log.i("values", "zhi:"+zhi.size());if(pos<list.size()){scroll.removeAllViews();//layout.removeViewAt(1);}else{btnx.setEnabled(false);btn_right.setVisibility(View.VISIBLE);}addViews();}});btns.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubzhi.remove(pos-1);pos--;Log.i("values", "pos:"+(pos));Log.i("values", "zhi:"+zhi.size());scroll.removeAllViews();//layout.removeViewAt(1);addViews();btn_right.setVisibility(View.GONE);}});danlayout.addView(titles);danlayout.addView(title);danlayout.addView(radioGroup);danlayout.addView(btn);danlayout.addView(et);ln.addView(btns);ln.addView(btnx);danlayout.addView(ln);scroll.addView(danlayout);//layout.addView(scroll);}}else{Toast.makeText(MainActivity.this, "题已完成,请提交!", 1).show();}}@Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    }

xml布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/ll"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:id="@+id/title"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="#000000" >        <RelativeLayout            android:layout_width="fill_parent"            android:layout_height="60dp"            android:background="@drawable/abg"            android:orientation="horizontal" >            <Button                android:id="@+id/btn_cancle"                android:text="返回"                android:textSize="15sp"                style="@style/BtnLeft" />            <TextView                android:id="@+id/tv_title"                style="@style/TitleText"                android:text="问卷调查" />                       <Button                android:id="@+id/btn_right"                style="@style/BtnCload"                                android:padding="10dp"                android:text="提交" />            <TextView                android:id="@+id/tv_sum"                style="@style/BtnCload"                android:background="@null"                android:layout_marginRight="70dp"                android:textSize="15sp"                android:text="" />        </RelativeLayout>            </LinearLayout> <ScrollView     android:id="@+id/scroll"      android:layout_width="match_parent"     android:layout_height="wrap_content"></ScrollView> </LinearLayout>

风格style

<style name="TitleText"><item name="android:layout_width">fill_parent</item><item name="android:layout_height">wrap_content</item><item name="android:textSize">20sp</item><item name="android:textStyle">bold</item><item name="android:singleLine">true</item><item name="android:textColor">#ffffff</item><item name="android:layout_centerVertical">true</item><item name="android:gravity">center</item><item name="android:paddingTop">3sp</item></style><style name="BtnLeft"><item name="android:layout_width">wrap_content</item><item name="android:layout_height">wrap_content</item><item name="android:textColor">#ffffff</item><item name="android:textSize">13sp</item><item name="android:layout_centerVertical">true</item><item name="android:layout_gravity">left|center_vertical</item><item name="android:layout_marginLeft">5dip</item></style>






0 0