如何使用动态创建的布局同时更新Edittext?

时间:2015-11-19 09:26:42

标签: android view android-edittext keylistener textwatcher

public class EditBoxActivity extends Activity {

    LinearLayout main;
    Button add;
    final Calendar c = Calendar.getInstance();
    TextWatcher twIncluded,twExcluded;
    String freq_item = "2";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_edit_box);

        main = (LinearLayout) findViewById(R.id.main);
        add = (Button) findViewById(R.id.add);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                addRow(main.getChildCount(), "");

            }
        });

    }

    private void addRow(int position, String id) {


        View hiddenInfo = getLayoutInflater().inflate(R.layout.edit_layout, main, false);

        final EditText edit_duration = (EditText) hiddenInfo.findViewById(R.id.text1);
        final EditText edit_qnty = (EditText) hiddenInfo.findViewById(R.id.text2);

        twIncluded = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {

                    String dur_val = s.toString();
                    int total = Integer.parseInt(freq_item) * Integer.parseInt(dur_val);
                    edit_qnty.removeTextChangedListener(twExcluded);
                    edit_qnty.setText(String.valueOf(total));
                    edit_qnty.addTextChangedListener(twExcluded);

                } catch (NumberFormatException e) {
                    e.printStackTrace();
//                    edit_qnty.setText("");
                } catch (StackOverflowError e) {
                    e.printStackTrace();
                }catch(ArrayIndexOutOfBoundsException e){
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

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

            @Override
            public void afterTextChanged(Editable s) {
            }
        };

        twExcluded = new TextWatcher()
        {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    String dur_val = s.toString();
                    int total = Integer.parseInt(dur_val) / Integer.parseInt(freq_item);
                    edit_duration.removeTextChangedListener(twIncluded);
                    edit_duration.setText(String.valueOf(total));
                    edit_duration.addTextChangedListener(twIncluded);

                }catch (NumberFormatException e){
                    e.printStackTrace();
//                    edit_duration.setText("");
                }catch (StackOverflowError e){
                    e.printStackTrace();
                }catch(ArrayIndexOutOfBoundsException e){
                    e.printStackTrace();
                }
                catch (Exception e){
                    e.printStackTrace();
                }

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

            @Override
            public void afterTextChanged(Editable s) {
            }
        };

        edit_duration.addTextChangedListener(twIncluded);
        edit_qnty.addTextChangedListener(twExcluded);


        main.addView(hiddenInfo);

    }
}

content_edit_box.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="test.sample.com.myapplication.DatepickerActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"

        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <Button
                android:id="@+id/add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="200dp"
                android:text="Add" />


            <LinearLayout
                android:id="@+id/main"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">


            </LinearLayout>
        </LinearLayout>

    </ScrollView>
</LinearLayout>

edit_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >


    <EditText android:id="@+id/text1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:maxLength="2"

        />


    <EditText android:id="@+id/text2"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:maxLength="2"

        />

</LinearLayout>

问题:

我有两个编辑框的动态布局。如果我点击按钮,动态布局反之亦然。所以每个布局包含两个编辑框。我在EditText中有一些计算。

这里我使用Textwatcher更新每个文本中的值。如果我更改了一个值,则另一个edittext同时更新。

我的问题是: 如果我逐个布局更新两个edittext中的值,它就会更新。例如,如果我更新第二行edittext并再次更新第一行edittext,则edittext的键侦听器将强制关闭。

我遇到以下异常。

  

11-19 14:46:26.073 12543-12543 / com.example.santosh.myapplication W / System.err:       11-19 14:46:26.073 12543-12543 / com.example.santosh.myapplication W / System.err:java.lang.StackOverflowError       11-19 14:46:26.077 12543-12543 / com.example.santosh.myapplication W / System.err:at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1013)       11-19 14:46:26.077 12543-12543 / com.example.santosh.myapplication W / System.err:at android.text.SpannableStringBuilder.getChars(SpannableStringBuilder.java:913)       11-19 14:46:26.077 12543-12543 / com.example.santosh.myapplication W / System.err:at android.text.TextUtils.getChars(TextUtils.java:79)       11-19 14:46:26.077 12543-12543 / com.example.santosh.myapplication W / System.err:at android.text.method.ReplacementTransformationMethod $ ReplacementCharSequence.getChars(ReplacementTransformationMethod.java:151)       11-19 14:46:26.077 12543-12543 / com.example.santosh.myapplication W / System.err:at android.text.TextUtils.getChars(TextUtils.java:79)       11-19 14:46:26.078 12543-12543 / com.example.santosh.myapplication W / System.err:at android.text.MeasuredText.setPara(MeasuredText.java:106)       11-19 14:46:26.078 12543-12543 / com.example.santosh.myapplication W / System.err:at android.text.StaticLayout.generate(StaticLayout.java:250)       11-19 14:46:26.078 12543-12543 / com.example.santosh.myapplication W / System.err:at android.text.DynamicLayout.reflow(DynamicLayout.java:284)       11-19 14:46:26.078 12543-12543 / com.example.santosh.myapplication W / System.err:at android.text.DynamicLayout。(DynamicLayout.java:170)       11-19 14:46:26.078 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.TextView.makeSingleLayout(TextView.java:6144)       11-19 14:46:26.078 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.TextView.makeNewLayout(TextView.java:6042)       11-19 14:46:26.078 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.TextView.checkForRelayout(TextView.java:6581)       11-19 14:46:26.079 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.TextView.setText(TextView.java:3823)       11-19 14:46:26.079 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.TextView.setText(TextView.java:3681)       11-19 14:46:26.079 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.EditText.setText(EditText.java:80)       11-19 14:46:26.079 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.TextView.setText(TextView.java:3656)       java.lang.StackOverflowError的       11-19 14:46:26.077 12543-12543 / com.example.santosh.myapplication W / System.err:at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1013)       11-19 14:46:26.077 12543-12543 / com.example.santosh.myapplication W / System.err:at android.text.SpannableStringBuilder.getChars(SpannableStringBuilder.java:913)       11-19 14:46:26.077 12543-12543 / com.example.santosh.myapplication W / System.err:at android.text.TextUtils.getChars(TextUtils.java:79)       11-19 14:46:26.077 12543-12543 / com.example.santosh.myapplication W / System.err:at android.text.method.ReplacementTransformationMethod $ ReplacementCharSequence.getChars(ReplacementTransformationMethod.java:151)       11-19 14:46:26.077 12543-12543 / com.example.santosh.myapplication W / System.err:at android.text.TextUtils.getChars(TextUtils.java:79)       11-19 14:46:26.078 12543-12543 / com.example.santosh.myapplication W / System.err:at android.text.MeasuredText.setPara(MeasuredText.java:106)       11-19 14:46:26.078 12543-12543 / com.example.santosh.myapplication W / System.err:at android.text.StaticLayout.generate(StaticLayout.java:250)       11-19 14:46:26.078 12543-12543 / com.example.santosh.myapplication W / System.err:at android.text.DynamicLayout.reflow(DynamicLayout.java:284)       11-19 14:46:26.078 12543-12543 / com.example.santosh.myapplication W / System.err:at android.text.DynamicLayout。(DynamicLayout.java:170)       11-19 14:46:26.078 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.TextView.makeSingleLayout(TextView.java:6144)       11-19 14:46:26.078 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.TextView.makeNewLayout(TextView.java:6042)       11-19 14:46:26.078 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.TextView.checkForRelayout(TextView.java:6581)       11-19 14:46:26.079 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.TextView.setText(TextView.java:3823)       11-19 14:46:26.079 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.TextView.setText(TextView.java:3681)       11-19 14:46:26.079 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.EditText.setText(EditText.java:80)       11-19 14:46:26.079 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.TextView.setText(TextView.java:3656)       11-19 14:46:26.080 12543-12543 / com.example.santosh.myapplication W / System.err:at com.example.santosh.myapplication.EditBoxActivity $ 3.onTextChanged(EditBoxActivity.java:97)       11-19 14:46:26.080 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.TextView.sendOnTextChanged(TextView.java:7418)       11-19 14:46:26.080 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.TextView.setText(TextView.java:3826)       11-19 14:46:26.080 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.TextView.setText(TextView.java:3681)       11-19 14:46:26.080 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.EditText.setText(EditText.java:80)       11-19 14:46:26.080 12543-12543 / com.example.santosh.myapplication W / System.err:at android.widget.TextView.setText(TextView.java:3656)       11-19 14:46:48.636 12543-12543 / com.example.santosh.myapplication W / IInputConnectionWrapper:非活动InputConnection上的getSelectedText       11-19 14:46:48.221 12543-12543 / com.example.santosh.myapplication W / System.err:at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)       11-19 14:46:48.221 12543-12543 / com.example.santosh.myapplication W / System.err:at com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchKeyEvent(PhoneWindow.java:2048)       11-19 14:46:48.221 12543-12543 / com.example.santosh.myapplication W / System.err:at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1510)       11-19 14:46:48.221 12543-12543 / com.example.santosh.myapplication W / System.err:at android.app.Activity.dispatchKeyEvent(Activity.java:2432)       11-19 14:46:48.221 12543-12543 / com.example.santosh.myapplication W / System.err:at com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchKeyEvent(PhoneWindow.java:1975)       11-19 14:46:48.221 12543-12543 / com.example.santosh.myapplication W / System.err:at android.view.ViewRootImpl $ ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4013)       11-19 14:46:48.221 12543-12543 / com.example.santosh.myapplication W / System.err:at android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:3987)       11-19 14:46:48.221 12543-12543 / com.example.santosh.myapplication W / System.err:at android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3553)       11-19 14:46:48.221 12543-12543 / com.example.santosh.myapplication W / System.err:at android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3603)       11-19 14:46:48.221 12543-12543 / com.example.santosh.myapplication W / System.err:at android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3572)

你能解决这个问题吗?

0 个答案:

没有答案