[英]ScrollView doesn't scroll up to the top after going through some edittext boxes using the next button on soft keyboard
[英]using scrollview to show button behind edittext keyboard
我有一個布局,其中有一個圖像,文本視圖,編輯框和一個按鈕,因此無論何時打開鍵盤,我都希望屏幕向下滾動到該按鈕,以便用戶始終可以鍵入並按Submit按鈕。 我確實通過使用scrollview做到了這一點,但后來我移除了滾動視圖,但做了一些更改,現在當我再次使用滾動視圖時,它不起作用。 這是代碼,請檢查。
layout_file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="myapp.anis.hotel.MainActivity"
android:background="@drawable/backgroundhotel"
android:fitsSystemWindows="true">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scroll">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Please share your feedback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView6"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="@android:color/background_dark"
android:fontFamily="serif"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
<TextView
android:text="@string/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/thanks"
android:shadowColor="@android:color/background_light"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColorHighlight="@android:color/background_dark"
android:textColor="@android:color/background_dark"
android:fontFamily="serif"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
<ImageView
android:layout_width="wrap_content"
app:srcCompat="@drawable/hotellogo"
android:id="@+id/imageView"
android:layout_height="150dp"
android:layout_marginTop="15dp"
android:layout_below="@+id/thanks"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:ems="10"
android:maxLines="50"
android:background="@android:drawable/editbox_background"
android:layout_marginTop="15dp"
android:id="@+id/feedback"
android:hint="Enter FeedBack"
android:inputType="textMultiLine"
android:gravity="left"
android:textColorHint="@android:color/background_dark"
android:textColor="@android:color/background_dark"
android:textColorHighlight="@android:color/background_dark"
android:layout_below="@+id/textView6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_height="180dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/submitting1"
android:background="@android:color/transparent"
android:onClick="onSubmit"
android:id="@+id/submit"
android:layout_below="@id/feedback"
android:layout_alignParentBottom="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
在我的主要活動中,我使用scrollto方法轉到“圖像”按鈕。
public class MainActivity extends AppCompatActivity {
ImageButton submit;
EditText feedBack;
static public String saveFeedBack="";
ScrollView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (ImageButton) findViewById(R.id.submit);
feedBack = (EditText) findViewById(R.id.feedback);
view = (ScrollView) findViewById(R.id.scroll);
view.scrollTo(submit.getScrollX(),submit.getScrollY());
}
public void onSubmit(View view){
saveFeedBack = feedBack.getText().toString();
if(saveFeedBack.length() >=4) {
Intent intent = new Intent(this, Main3Activity.class);
startActivity(intent);
}
else{
Toast.makeText(getApplicationContext(),"Enter valid Feedback please.",Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onRestart() {
super.onRestart();
feedBack.setText("");
feedBack.setHint("Enter Feedback");
}
}
但是當我運行我的應用程序時,scrollview滾動到我的編輯文本,而不是我的按鈕。 我已經嘗試了AdjustResize,adjustPan和所有這些東西,但是它們都不起作用。
確定,經過大量研究,我已經解決了您的問題。 實際上,事情是當您使用滾動到onCreate中的任何位置時,它不起作用,因為需要很小的延遲才能使視圖膨脹。
因此,在您的onCreate中使用以下代碼:
view.postDelayed(new Runnable() {
@Override
public void run() {
view.fullScroll(ScrollView.FOCUS_DOWN);
}
},200);
希望能幫助到你!!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.