2012-03-09 82 views
1

具有EditText小部件以及放置在ScrollView中的一些小部件。EditText滾動條和ScrollView

我用android:scrollbars =「vertical」設置了EditText的屬性,以便在editText裏面垂直滾動。

現在,當我啓動活動時,editText具有焦點,並且它顯示了幾秒鐘的垂直滾動條。

這裏的問題是當我嘗試在EditText中滾動時,ScrollView移動。

如何在EditText中啓用滾動而不在滾動視圖中。

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:id="@+id/main" 
     android:layout_width="match_parent"    
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 
     . 
     . 
     . 
     <EditText 
      android:id="@+id/smset" 
      android:layout_width="match_parent" 
      android:gravity="top|left" 
      android:height="100dip" 
      android:inputType="textMultiLine" > 
     </EditText> 
     . 
     . 
     . 
    </LinearLayout> 
</ScrollView> 

回答

2

您不能放置可以滾動的東西,也可以滾動內容。

當你觸摸屏幕時,Android不能確定你想要滾動哪個元素,所以根本不支持它。 (忽略一些非常糟糕的黑客行爲)。

+0

我需要你幫上滾動視圖u能幫助我對 – Developer 2013-08-06 10:22:06

+0

@Gaurav使一個問題,顯示你嘗試過,解釋你的問題,並使用適當的英語(而不是「你」應該是「你」等)。 – WarrenFaith 2013-08-06 10:24:24

+0

我已發佈此在http://stackoverflow.com/questions/18077726/scroll-view-is-not-working-properly-android請幫我在這 – Developer 2013-08-06 10:33:12

0

一個這樣的手段是:

scroll.setOnTouchListener(new OnTouchListener() 
{ 
    public boolean onTouch(View v, MotionEvent event) 
    { 
     return true; 
    } 
}); 

注意,爲了能夠在滾動型滾動,你必須運行相同的功能,但與「返回false;」代替。

0

如果做得不好,我不會說這是壞習慣,但這不是自然而然的預期。

我經常把可滾動的視圖放在滾動的東西里面,但是你必須鎖定外層並在裏面啓用滾動。理想情況下,你應該做別的,但這是可能的。

您可以修改ScrollView以鎖定,如here

然後編程鎖定LockableScrollView當內部物件被觸摸時滾動。

smset.setOnTouchListener(new OnTouchListener() 
{ 
    public boolean onTouch(View v, MotionEvent event) 
    { 
     // Disables LockableScrollView when the EditText is touched 
     if (event.getAction() == MotionEvent.ACTION_DOWN) 
     { 
      scrollView1.setScrollingEnabled(false); 
     } 

     // Enables LockableScrollView when the EditText is touched 
     if (event.getAction() == MotionEvent.ACTION_UP) 
     { 
      scrollView1.setScrollingEnabled(true); 
     } 
     return false; 
    } 
}); 

這種方法的缺點是,當EditText上被觸摸它,將禁用滾動,但它不應該引起任何其他副作用。

5

在你的java文件

 EditText dwEdit = (EditText) findViewById(R.id.DwEdit);  
    dwEdit.setOnTouchListener(new OnTouchListener() { 
      public boolean onTouch(View view, MotionEvent event) { 
       // TODO Auto-generated method stub 
       if (view.getId() ==R.id.DwEdit) { 
        view.getParent().requestDisallowInterceptTouchEvent(true); 
        switch (event.getAction()&MotionEvent.ACTION_MASK){ 
        case MotionEvent.ACTION_UP: 
         view.getParent().requestDisallowInterceptTouchEvent(false); 
         break; 
        } 
       } 
       return false; 
      } 
     }); 

在XML

<EditText 
    android:id="@+id/DwEdit" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:minLines="10" 
    android:scrollbarStyle="insideInset" 
    android:scrollbars="vertical" 
    android:overScrollMode="always" 
    android:inputType="textCapSentences"> 
    </EditText>