ScrollView прокручивать вверх при наборе
Во-первых, у меня есть несколько EditTexts в Framelayout, который обернут ScollView.
Я уже использую код ниже, чтобы позволить ScrollView прокручиваться вверх, когда отображается клавиатура -
((Activity) getContext()).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
Кроме того, я попробовал ADJUST_PAN в приведенном выше коде, и ничего не изменилось.
Вот в чем проблема - когда я набираю один из этих EditTexts, ScrollView прокручивается автоматически, что приводит к тому, что мой текущий EditText невидим.
Мне интересно, почему это произошло? Мое текущее редактирование EditText теряет фокус при наборе текста на клавиатуре? И как мне это исправить?
Кстати, я не использовал xml для макета. Все макеты написаны на Java.
Основной макет -
public class PoorEditScroll extends ScrollView {
PoorEdit poorEdit;
public PoorEditScroll(Context context) {
super(context);
initUI();
}
public PoorEditScroll(Context context, AttributeSet attrs) {
super(context, attrs);
initUI();
}
private void initUI(){
setScrollContainer(false);
poorEdit = new PoorEdit(getContext());
this.addView(poorEdit);
}
this.addView(poorEdit);
}
public void toJson(String s) {
poorEdit.toJson(s);
}
public void loadJson(String s) {
poorEdit.loadJson(s);
}
class PoorEdit extends LinearLayout {
private EditView editView;
public PoorEdit(Context context) {
super(context);
initUI();
}
public PoorEdit(Context context, AttributeSet attrs) {
super(context, attrs);
initUI();
}
private void initUI(){
this.setOrientation(VERTICAL);
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
this.setLayoutParams(layoutParams);
DisplayMetrics dm = getContext().getResources().getSystem().getDisplayMetrics();
this.setMinimumHeight(dm.heightPixels);
// TODO: 15/11/25 Override OnMeasure in EditView
editView = new EditView(getContext());
this.addView(editView);
((Activity) getContext()).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
public void loadJson(String folderPath){
editView.loadJson(folderPath);
}
public String toJson(String dest){
return editView.toJson(dest);
}
}
И EditView
является -
public class EditView extends FrameLayout implements View.OnDragListener {
public static BaseContainer poorBoy;
private BaseContainer lastAdd = null;
public EditView(Context context) {
super(context);
initUI();
}
public EditView(Context context, AttributeSet attrs) {
super(context, attrs);
initUI();
}
private void initUI(){
this.setOnDragListener(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);
this.setLayoutParams(layoutParams);
//Call addView to add a EditText in this function.
addTextOn(0, 0, Constants.eleWidth, Constants.eleHeight);
}
}
1 ответ
Добавьте этот код в свой <activity>
в AndroidManifest.xml
android:windowSoftInputMode="adjustResize|stateHidden"