android 将 EditText 设置为水平滚动而不是垂直滚动

android setting EditText to scroll horizontally instead of vertically

我正在以编程方式添加一个 editText 控件,该控件的值比控件的宽度长,并且换行到下一行。 我希望文本改为水平阅读,而不是跨越多行或允许垂直滚动

我尝试了以下设置的多种组合,但它们似乎根本没有影响。

et.setHorizontallyScrolling(true);
et.setSingleLine(true);
et.setLines(1);
et.setMaxLines(1);
et.setHorizontalScrollBarEnabled(true);

et.setRawInputType(InputType.TYPE_CLASS_TEXT| InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE);

只需更改您的代码....借助此

 public class MainActivity extends Activity {
    private LinearLayout ll;
    private ScrollView sv;
    private EditText et,et1,et2,et3;

    @Overrid
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        et = new EditText(this);
        et.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        et.setHeight(80);

        sv = new ScrollView(this);
        sv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        sv.addView(et);

        ll.addView(sv);

        setContentView(ll);
    }
}

删除所有,仅使用 et.setSingleLine(true);

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    ll.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
    EditText et = new EditText(this);
    et.setSingleLine(true);
    et.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
    et.setRawInputType(InputType.TYPE_CLASS_TEXT| InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE);
    ll.addView(et);
    setContentView(ll);
}

仅使用 setSingleLine(true); 这很好用。