0

我在 Android 中使用了一个相当简单的自定义寻呼机适配器,我想跟踪编辑文本字段的内容,以便每张新幻灯片在输入时都保留相同的文本。有人对此有任何见解吗?我猜我需要为文本更改设置一个侦听器,但我不确定该放在哪里。这是我的适配器:

private class CustomPagerAdapter extends PagerAdapter {

    public int getCount() {
        return 3;
    }

    public Object instantiateItem(ViewGroup collection, int position) {

        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(LAYOUT_INFLATER_SERVICE);
        View view;

        switch(position) {
            case 0:
                view = inflater.inflate(R.layout.slide0, null, false);
                break;
            case 1:
                view = inflater.inflate(R.layout.slide1, null, false);
                break;
            case 2:
                view = inflater.inflate(R.layout.slide2, null, false);
                break;
            default:
                view = inflater.inflate(R.layout.slide0, null, false);
        }
        collection.addView(view,0);

        return view;
    }



    @Override
    public void destroyItem(ViewGroup arg0, int arg1, Object arg2) {
        arg0.removeView((View) arg2);
    }
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }
    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
        mCurrentSlide = (View) object;

    }
}
4

3 回答 3

1

好的,我是这样做的:

在你的主类中设置一个成员变量:

mText = "";

在您的适配器类的 instantiateItem() 中设置一个 textWatcher:

EditText edtSlide = (EditText)view.findViewById(R.id.edtSlide);
//Set a listener for the editText that updates the member variable on keystroke.
    TextWatcher tw = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mText = s.toString();
        }

        };

    edtSlide.addTextChangedListener(tw);

然后在适配器类的 setPrimaryItem() 方法中:

@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
    super.setPrimaryItem(container, position, object);
    EditText text = (EditText)((View) object).findViewById(R.id.edtSlide);
    text.setText(mText);
}
于 2013-08-21T19:23:59.967 回答
0

创建一个 TextWatcher,然后将其添加到每个 EditText:

TextWatcher tw = new TextWatcher() {

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

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

            @Override
            public void afterTextChanged(Editable s) {
            // Save the text to some global variable
            }
        };

EditText et = (EditText) findViewById(R.id.editText1);
EditText et2 = (EditText) findViewById(R.id.editText2);

et.addTextChangedListener(tw);
et2.addTextChangedListener(tw);

编辑:

在上面的代码中,没有简单的方法可以知道是哪个 EditText 调用了事件。在这种情况下,您需要为每个 EditText 提供自己的 TextWatcher:

et1.addTextChangedListener(new TextWatcher() {

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

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

        @Override
        public void afterTextChanged(Editable s) {
            global_string_variable_for_et1 = s.toString();
        }
    });

编辑2:

TextWatcher[] tw = new TextWatcher[12];
for (int i = 0; i < tw.length; i++) {
tw[i] = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        @Override
        public void afterTextChanged(Editable s) {
        // Save the text to some global variable array
        txt_array[i] = s.toString();
        }
    };
}

for (int i = 0; i < et.length; i++) {7
    et[i].addTextChangedListener(tw[i]);
}
于 2013-08-13T20:12:53.100 回答
0

我可以通过放置一个 TextWatcher 来获得它,如下所示。

notes = (EditText) itemView.findViewById(R.id.notesText);

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            notedEnteredByUser = charSequence.toString();
            Log.i("TEXTWATCHER", notedEnteredByUser);
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };
    notes.addTextChangedListener(textWatcher);

在 instanceiteItem 中,我使用的是字符串 notedEnteredByUser。

于 2016-08-04T03:50:08.223 回答