2017-06-07 28 views
0

我有一個佈局,其中我有一個圖像,textview,編輯框和一個按鈕,所以每當鍵盤打開我想我的屏幕向下滾動到按鈕,以便用戶可以隨時鍵入並按提交按鈕。我這樣做是通過使用滾動視圖,但我刪除滾動視圖做了一些改變,現在當我再次使用滾動視圖它不起作用。 這裏是代碼請檢查。使用滾動視圖來顯示edittext鍵盤背後的按鈕

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"); 
} 

}

但是當我運行我的應用程序滾動視圖滾動到我的編輯文本,但不是我的按鈕。我嘗試了adjustResize,adjustPan和所有這些東西,但都沒有工作。

回答

0

經過大量的研究,我已經解決了您的問題。事實上,當你使用滾動到onCreate內的任何位置時,它不起作用,因爲膨脹視圖需要很小的延遲。

因此,使用下面的代碼,您的onCreate內:

view.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       view.fullScroll(ScrollView.FOCUS_DOWN); 
      } 
     },200); 

希望它幫助!

+0

我試過這個,但它沒有工作仍然是相同的結果。當我點擊編輯文本,鍵盤顯示滾動視圖滾動到編輯文本,按鈕在鍵盤後面。 – user2307659

+0

嘗試給paddingBottom,而不是邊距,並嘗試增加填充,因爲我解決了我的問題相同的方式 – sumit

+0

我想這樣做,但滾動視圖只滾動到編輯文本,並沒有低於這一點。 – user2307659

相關問題