У меня есть ScrollView, который имеет линейный макет, Когда я создаю и добавляю EditText программно в макет, мой код должен прокручиваться вниз, но затем фокус переходит на второе снизу поле вместо того, что я хочу, чтобы он фокусировал нижнее поле, и я могу '' Не понимаю, почему.
Вот код:
public void addPlayer(View view){
ScrollView scrollview = (ScrollView) findViewById(R.id.PlayerAddScroll);
EditText tv = new EditText(this);
tv.setHint("Player " + (playerCount + 1));
playerCount++;
tv.setSingleLine(true);
LinearLayout playerListView = (LinearLayout) findViewById(R.id.playerListView);
playerListView.addView(tv);
playerFields.add(tv);
scrollview.post(scrollBottom);
}
Это то, что делает scrollBottom
Runnable scrollBottom=new Runnable() {
@Override
public void run() {
ScrollView scrollView = (ScrollView) findViewById(R.id.PlayerAddScroll);
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
};
Мой код вызывает это (не фокусируясь на последнем поле):
@Pavlus Я пробовал это после scrollview.fullScroll (), но ничего не сделал ...




Нашел исправление, добавив android:paddingBottom = "5dp" в мой LinearLayout в XML.
измените свой код вот так, и он будет работать нормально!
Runnable scrollBottom = new Runnable() {
@Override
public void run() {
//This linear is the linearLayout which you added your textView to it
LinearLayout playerListView = (LinearLayout) findViewById(R.id.ll);
playerListView.getChildAt(playerListView.getChildCount()-1).requestFocus();
}
};
Я попытался динамически запросить фокус для последнего EditText после метода fullScroll. код находится в Runnable scrollBottom
ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
EditText editText = (EditText) linearLayout.getChildAt(linearLayout.getChildCount() - 1);
editText.requestFocus();
tv.requestFocus()- stackoverflow.com/questions/8077755/edittext-request-focus