1 importandroid.content.Context;2 importandroid.graphics.Color;3 importandroid.graphics.Paint;4 importandroid.text.Editable;5 importandroid.text.TextWatcher;6 importandroid.view.Gravity;7 importandroid.view.View;8 importandroid.widget.EditText;9 importandroid.widget.LinearLayout;10 importandroid.widget.ScrollView;11 importandroid.widget.TextView;12
13 importcom.szjpsj.common.util.UtilJson;14
15 importorg.json.JSONArray;16 importorg.json.JSONObject;17
18 importjava.util.List;19
20 /**
21 * 输入选择框22 */
23 public class MyInputSelection extends LinearLayout implementsTextWatcher24 {25 private ScrollView scrollView = null;//用于选择框中,可固定高度,用于滚动
26 private int scrollHight = 0;27 public void setScrollHight(intscrollHight){28 this.scrollHight =scrollHight;29 }30 private LinearLayout selectOptionLine = null ;//用于选择项
31 private EditText myEditText = null;//输入框
32 private ListoptionList ;33 //34 private String addRemark;//添加描述
35 privateInputSelection.AddListener addListener;36 public voidsetAddListener(String addRemark, InputSelection.AddListener addListener) {37 this.addRemark =addRemark;38 this.addListener =addListener;39 }40
41 public voidsetOptionList(List list) {42 optionList =list ;43 }44 45 publicMyInputSelection(Context context){46 super(context);47 init();48 }49
50 private voidinit(){51 LinearLayout linearLayout = newLinearLayout(getContext());52 linearLayout.setOrientation(VERTICAL);//设置数据
53 myEditText = newEditText(getContext());54 myEditText.addTextChangedListener(this);55 myEditText.setOnFocusChangeListener(newView.OnFocusChangeListener() {56 @Override57 public void onFocusChange(View v, booleanhasFocus) {58 if(hasFocus) {59 //获得焦点
60 showOptions(myEditText.getText().toString());61 }62 }63 });64 linearLayout.addView(myEditText,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);65 /
66 scrollView = newScrollView(getContext());67 68 selectOptionLine = newLinearLayout(getContext());69 selectOptionLine.setOrientation(VERTICAL);70 selectOptionLine.setPadding(15,5,15,5);71 scrollView.addView(selectOptionLine,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);72 scrollView.setVisibility(GONE);//设置隐藏
73 if(scrollHight>0){74 linearLayout.addView(scrollView, LayoutParams.MATCH_PARENT,scrollHight);75 }else{76 linearLayout.addView(scrollView, LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);77 }78 /
79 addView(linearLayout,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);//添加
80 }81 publicString getText(){82 returnmyEditText.getText().toString();83 }84 public voidsetText(String text) {85 try{86 myEditText.setText(text);87 myEditText.setSelection(text.length());88 scrollView.setVisibility(GONE);//设置隐藏
89 }catch(Exception e){}90 }91 public void setTextColor(intcolor){92 myEditText.setTextColor(color);//设置颜色
93 }94 public voidsetHit(String hitStr){95 myEditText.setHint(hitStr);96 }97 public void setTextSize(inttextSize){98 myEditText.setTextSize(textSize);99 }100 101 @Override102 public void beforeTextChanged(CharSequence s, int start, int count, intafter) {103
104 }105
106 @Override107 public void onTextChanged(CharSequence s, int start, int before, intcount) {108
109 }110
111 @Override112 public voidafterTextChanged(Editable s) {113 showOptions(s.toString());114 }115 private voidshowOptions(String s){116 scrollView.setVisibility(VISIBLE);//设置显示
117 selectOptionLine.removeAllViews();118 boolean isIn = false;119 for(int i=0;optionList!=null&&i=0){123 isIn = true;//存在
124 if(str.equals(s.toString())){125 continue;126 }127 TextView textView = newTextView(getContext());128 textView.setPadding(0,2,0,5);129 textView.setText(str);130 textView.setTextSize(18);131 if(i%2==0){132 textView.setBackgroundColor(Color.parseColor("#88F0E6B4"));//选择区域背景颜色,可以根据自己需要设置,或引出外层接口
133 }else{134 textView.setBackgroundColor(Color.parseColor("#88C7F3B6"));135 }136 textView.setOnClickListener(newView.OnClickListener() {137 @Override138 public voidonClick(View v) {139 try{140 setText(((TextView)v).getText().toString());141 }catch(Exception e){}142 }143 });144 selectOptionLine.addView(textView,LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);145 TextView view = new TextView(this.getContext());146 view.setBackgroundColor(Color.BLACK);//黑色分割线,可以换颜色,或引入到外层接口
147 selectOptionLine.addView(view,LinearLayout.LayoutParams.MATCH_PARENT, 2);148 }149 }catch(Exception e){}150 }//end for
151 if(!isIn){152 isAddTip();153 }154 }155 private voidisAddTip(){156 if(addListener==null) return;157 if(addRemark==null||addRemark.equals("")) return;//未设置 无需添加
158 try{159 if(myEditText.getText().toString().length()<=0) return;//最终结果为空,不添加
160 LinearLayout linearLayout = new LinearLayout(this.getContext());161 linearLayout.setGravity(Gravity.RIGHT);162 TextView textView = new TextView(this.getContext());163 textView.setText(addRemark);164 textView.setTextSize(20);165 textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
166 textView.setOnClickListener(newView.OnClickListener(){167 @Override168 public voidonClick(View v) {169 addListener.addOnClick(myEditText.getText().toString());//添加
170 selectOptionLine.removeAllViews();//清除选择区控件数据
171 }172 });173 linearLayout.addView(textView,LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);174 selectOptionLine.addView(linearLayout,LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);175 }catch(Exception e){}176 }177 /
178 public interfaceAddListener{179 public voidaddOnClick(String text);180 }181 }