我在Android中使用了一个相当简单的自定义寻呼机适配器,我想跟踪edittext字段的内容,以便每个新幻灯片在输入时保留相同的文本。有人对此有任何见解吗?我猜我需要设置一个用于文本更改的监听器,但我不确定在哪里放置它。这是我的适配器:
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;
}
}
答案 0 :(得分: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);
}
答案 1 :(得分: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();
}
});
<强> EDIT2:强>
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]);
}
答案 2 :(得分: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);
在instantiateItem中,我正在使用String notedEnteredByUser。