代码如何根据给编辑文本视图的计数值动态实现线性布局

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代码如何根据给编辑文本视图的计数值动态实现线性布局相关的知识,希望对你有一定的参考价值。

我正在制作个人详细信息模型,有2个无线电选项已婚和未婚。点击已婚单选按钮,它将启用编辑文本,它将采取该已婚人士的子女数量。基于输入的编辑文本值。动态线性布局正在实施。但问题是当点击编辑文本时它正在创建线性布局(在删除编辑文本时,线性布局也应该擦除)。

我的代码是这样的。在主要活动中

et_childCount.addTextChangedListener(new TextWatcher() {
   @Override
       public void beforeTextChanged(CharSequence s, int start, int count, int after) {
   @Override
       public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    @Override
       public void afterTextChanged(Editable s) {
           if (s != null) {
               countI = childCount;
               noOfChild = countI;
                if (noOfChild >= 1) {
                   for (int i = 1; i <= noOfChild; i++) {
                       LinearLayout linearLayout = new LinearLayout(getActivity());
                       LayoutParams params = new LinearLayout.LayoutParams(
                               LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                       linearLayout.setLayoutParams(params);
                       linearLayout.setOrientation(LinearLayout.VERTICAL);
                       TextView tv_BorderChildDetails = new TextView(getActivity());
                       tv_BorderChildDetails.setText("Child " + i);
                       tv_BorderChildDetails.setGravity(Gravity.CENTER_HORIZONTAL);
                       tv_BorderChildDetails.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                       linearLayout.addView(tv_BorderChildDetails);
                       EditText et_cName = new EditText(getActivity());
                       EditText et_cDob = new EditText(getActivity());
                       EditText et_cSchoolName = new EditText(getActivity());
                       et_cName.setHint("Child name");
                       et_cDob.setHint("Date of birth");
                       et_cSchoolName.setHint("School name");
                       childNameList.add(et_cName);
                       childDOBList.add(et_cDob);
                       childSchoolNameList.add(et_cSchoolName);
                       et_cName.setLayoutParams(params);
                       et_cDob.setLayoutParams(params);
                       et_cSchoolName.setLayoutParams(params);
                        linearLayout.addView(et_cName);
                       linearLayout.addView(et_cDob);
                       linearLayout.addView(et_cSchoolName); } } });
答案

您可以像这样使用TextWatcher()

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {}

    @Override
    public void afterTextChanged(Editable s) {
        // Convert the editable to int.
        int userInput = Integer.parseInt(s.getText().toString());
        // First we want to copy the data ArrayList so we don't affect the original one.
        ArrayList<Data> new = copyArray(originalDataArrayList);
        // Then we want to run a for loop to change the item count depending on the userInput value.
        for (int i = 0; i < newArray.size(); i++){
          if (i >= userInput){
            newArray.remove(i);
        }
      }
        // Now we have ArrayList with the required size.
        // Here you create a new adapter and pass the new ArrayList to it and set the ListView adapter as the new adapter.
    }
});

复制ArrayList方法:

    private ArrayList<Data> copyArray(ArrayList<Data> array) {
    ArrayList<Data> newArray = new ArrayList<>();
    newArray.addAll(array);
    return newArray;
}
另一答案

您可以使用addTextChangedListener,它是EditText的属性。这是代码。

YourEditText.addTextChangedListener(new TextWatcher() {

    @Override
    public void afterTextChanged(Editable s) {
        int count = Integer.parseInt(YourEditText.getText().toString());
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start,
    int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start,
    int before, int count) {

});

将编辑文本输入类型更改为数字,否则如果有人输入文本,您将面临错误。

Listview listview= (ListView) findViewById(R.id.your_list_view);
Adapter adapter = new adapter (YourActivity.this, count);
listview.setAdapter(adapter );

只需在计数部分输入您的列表视图计数,这里是适配器类

 public class Adapter extends BaseAdapter {

    // Declare Variables \
    Context mContext;
    LayoutInflater inflater;
    int count;

    public Adapter (Context context, String count) {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
        this.count = Integer.parseInt(count);
        //---------------------------\
    }

    public class ViewHolder {

    }

    @Override
    public int getCount() {
        return count; // Your Desired Count which you entered in EditText \
    }

    @Override
    public Object getItem(int position) {
        return null; 
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.lv_items, null);
            yourTv = view.findViewById(R.id.lv_item_tv);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        return view;
    }}

每当您更改列表视图计数时,您必须初始化适配器和列表视图。希望能帮助到你!

以上是关于代码如何根据给编辑文本视图的计数值动态实现线性布局的主要内容,如果未能解决你的问题,请参考以下文章

如何根据字符串值禁用列表视图项?

如何实现在动态布局中添加更多视图在android中实现滚动视图

Android水平线性布局 - 包装元素[重复]

将 TextView 动态水平添加到等权线性布局

如何在 android 中实用地添加和删除布局?

如何根据其中的内容创建具有动态高度的集合视图?