Android自动滚动EditText提示

时间:2012-07-07 08:14:18

标签: android android-edittext

problem with making edittext scrollable

提到它不能用singleLine = true滚动。

所以我有两个选择:

  1. 我检查用户输入“\ n”并删除它,然后我也要担心第二行的内容。

  2. 我使用循环滚动的自定义文本视图,这里是否也可以仅用于提示?

     <com.test.ScrollingTextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginTop="1dp"
            android:layout_weight="0.17"
            android:background="@color/black"
            android:ellipsize="marquee"
            android:gravity="center_vertical"
            android:marqueeRepeatLimit="marquee_forever"
            android:paddingLeft="10dp"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="@string/editLength"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/white" />
    
  3. 公共类ScrollingTextView扩展了TextView {

    public ScrollingTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    public ScrollingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public ScrollingTextView(Context context) {
        super(context);
    }
    
    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        if (focused)
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }
    
    @Override
    public void onWindowFocusChanged(boolean focused) {
        if (focused)
            super.onWindowFocusChanged(focused);
    }
    
    @Override
    public boolean isFocused() {
        return true;
    }
    

    }

    我不能让这种形式合适。对不起。

1 个答案:

答案 0 :(得分:2)

使您的EditText可滚动如下:

EditText yourEditText = new EditText(context);
yourEditText.setScroller(new Scroller(context)); 
yourEditText.setMaxLines(1); 
yourEditText.setVerticalScrollBarEnabled(true); 
yourEditText.setMovementMethod(new ScrollingMovementMethod());