抱歉我的英文。我有3个EditText,它们在ScrollView中。当您单击其中一个EditText时,ScrollView会下降,它会关注我们选择的EditText。但是,如果我单击任何三个EditText,它向下滚动并只关注EditText city
,无论我选择EditText。我不知道为什么,我问你的帮助
此 xml 我有3个editText city
,adress
和zip
<LinearLayout
android:layout_weight="0.1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#26ffffff"
android:layout_marginBottom="5dp"
>
<EditText
android:id="@+id/adress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@null"
android:layout_gravity="center"
android:ems="15"
android:gravity="center"
android:hint="Adress"
android:singleLine="true"
android:textColor="#c9c9c9"
android:textColorHint="#c9c9c9"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="0.2"
android:layout_marginRight="2.5dp"
android:background="#26ffffff"
android:layout_marginBottom="10dp"
>
<EditText
android:id="@+id/zip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:layout_gravity="center"
android:ems="15"
android:gravity="center"
android:hint="ZIP"
android:singleLine="true"
android:textColor="#c9c9c9"
android:textColorHint="#c9c9c9"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="2.5dp"
android:layout_weight="0.2"
android:background="#26ffffff"
android:layout_marginBottom="10dp"
>
<EditText
android:id="@+id/city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@null"
android:layout_gravity="center"
android:ems="15"
android:gravity="center"
android:hint="City"
android:singleLine="true"
android:textColor="#c9c9c9"
android:textColorHint="#c9c9c9"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
</LinearLayout>
我的代码
//adress = (EditText) findViewById(R.id.adress);
// zip = (EditText) findViewById(R.id.zip);
// city = (EditText) findViewById(R.id.city);
adress.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
isKeyboardShown(adress.getRootView(), adress);
zip.clearFocus();
city.clearFocus();
}});
zip.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
isKeyboardShown(zip.getRootView(), zip);
adress.clearFocus();
city.clearFocus();
}});
city.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
isKeyboardShown(city.getRootView(), city);
zip.clearFocus();
adress.clearFocus();
}});
//method, if keyboard show -> scroll down
private boolean isKeyboardShown(View rootView, TextView text) {
/* 128dp = 32dp * 4, minimum button height 32dp and generic 4 rows soft keyboard */
final int SOFT_KEYBOARD_HEIGHT_DP_THRESHOLD = 128;
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
DisplayMetrics dm = rootView.getResources().getDisplayMetrics();
/* heightDiff = rootView height - status bar height (r.top) - visible frame height (r.bottom - r.top) */
int heightDiff = rootView.getBottom() - r.bottom;
/* Threshold size: dp to pixels, multiply with display density */
boolean isKeyboardShown = heightDiff > SOFT_KEYBOARD_HEIGHT_DP_THRESHOLD * dm.density;
Log.e( "isKeyboardShown ? " + isKeyboardShown + ", heightDiff:" + heightDiff + ", density:" + dm.density
+ "root view height:" + rootView.getHeight() + ", rect:" + r, "sd");
//check keyboard show or not
if(isKeyboardShown) {
scroll.fullScroll(View.FOCUS_DOWN);
text.requestFocus();
} else {
}
return isKeyboardShown;
}
UDP
现在代码是
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
adress = (EditText) findViewById(R.id.adress);
zip = (EditText) findViewById(R.id.zip);
city = (EditText) findViewById(R.id.city);
scroll = (ScrollView) findViewById(R.id.scrollView3);
//down scroll view
downScroll();
//focuses edit text
focusEditText();
}
//its method i create
public void focusEditText() {
adress.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("click", "onMtouch");
adress.setFocusable(true);
adress.requestFocus();
zip.clearFocus();
city.clearFocus();
return false;
}
});
zip.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("click", "onMtouch");
zip.setFocusable(true);
zip.requestFocus();
adress.clearFocus();
city.clearFocus();
return false;
}
});
city.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("click", "onMtouch");
city.setFocusable(true);
city.requestFocus();
zip.clearFocus();
adress.clearFocus();
return false;
}
});
}
//scroll down
public void downScroll() {
adress.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
isKeyboardShown(adress.getRootView(), adress);
}});
zip.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
isKeyboardShown(zip.getRootView(), zip);
}});
city.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
isKeyboardShown(city.getRootView(), city);
}});
}
//method scroll down
private boolean isKeyboardShown(View rootView, EditText text) {
/* 128dp = 32dp * 4, minimum button height 32dp and generic 4 rows soft keyboard */
final int SOFT_KEYBOARD_HEIGHT_DP_THRESHOLD = 128;
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
DisplayMetrics dm = rootView.getResources().getDisplayMetrics();
/* heightDiff = rootView height - status bar height (r.top) - visible frame height (r.bottom - r.top) */
int heightDiff = rootView.getBottom() - r.bottom;
/* Threshold size: dp to pixels, multiply with display density */
boolean isKeyboardShown = heightDiff > SOFT_KEYBOARD_HEIGHT_DP_THRESHOLD * dm.density;
Log.e( "isKeyboardShown ? " + isKeyboardShown + ", heightDiff:" + heightDiff + ", density:" + dm.density
+ "root view height:" + rootView.getHeight() + ", rect:" + r, "sd");
if(isKeyboardShown) {
scroll.fullScroll(View.FOCUS_DOWN);
} else {
if (!adressText.equals("") && !cityText.equals("") && isOnline()) {
new GeocoderTask().execute(new String[]{cityText + "," + adressText});
}
}
return isKeyboardShown;
}