【问题标题】:How to attach an EditText to the end of a TextView如何将 EditText 附加到 TextView 的末尾
【发布时间】:2015-03-13 18:48:05
【问题描述】:

我正在编写一个类似终端的应用程序(实际上是一个 ssh 客户端)。我现在有一个TextView 输出响应和一个固定在底部的EditText 以键入命令。

我正在尝试将EditText 附加到TextView 输出的末尾(动态),就像在真实终端中一样:

[TextView]root@whatever:~#[EditText] |command

关于如何做到这一点的任何想法?

我将尝试澄清我要完成的工作。

我无法设置固定权重,因为随着更多字节从流中到达,更多文本被异步添加到TextView。 LinearLayout 解决方案没有将EditText 光标位置设置在TextView 输入的末尾。我尝试过RelativeLayout toRightOf,但EditText 只是在TextView 被填充时离开屏幕。

我想要什么:

【问题讨论】:

  • 你试过什么?
  • 使用例如水平方向的线性布局。将 TextView 和 EditText 放在里面。这取决于你需要什么,你也可以使用权重属性
  • 使用相对布局创建布局并在该xml中添加textview。创建动态编辑文本并将 android:layout_toRightOf="@+id/textview"

标签: android terminal console android-edittext textview


【解决方案1】:

通过在TextView(etShell) 上每次追加后更新EditText(etInput) 位置解决:

Layout layout = etShell.getLayout();
int pos = etShell.length();
int line = layout.getLineForOffset(pos);
int baseline = layout.getLineBaseline(line);
int ascent = layout.getLineAscent(line);
float x = layout.getPrimaryHorizontal(pos);
float y = baseline + ascent;
etInput.setMaxWidth(width - (int) x);
etInput.setX(x);
etInput.setY(y);

【讨论】:

    【解决方案2】:

    为避免在不同的屏幕尺寸上editText 被“吃掉”,最好使用layout_weight。例如:

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal" >
    
        <TextView
           android:id="@+id/yourTextView"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1" />
    
        <EditText
           android:id="@+id/yourEditText"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1" />
    
        </LinearLayout>
    

    这样,两个视图将具有相同的尺寸(长度、高度)。您可以使用 layout_weight 属性。如果您想让 TextView 大于 EditText,请将 textView 的权重设置为 0.25,将 EditText 的权重设置为 0.75。反之亦然。

    编辑

    如果您想要一个用户无法更改的静态文本,您可以执行以下操作:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       >
    
     <EditText
       android:id="@+id/yourEditText"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:layout_centerHorizontal="true"
       />
    
     <TextView
       android:id="@+id/yourTextView"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignLeft="@id/yourEditText"
       android:layout_alignBaseline="@+id/yourEditText"
       android:text="C:\" />
    
     </RelativeLayout>
    

    我目前无法测试它,但它应该会提示你如何做到这一点。

    【讨论】:

    • 感谢您的回答。但是与上述答案的结果相同。我需要 EditText 跟随 TextView 中输入的结尾,而不是固定的权重。
    • 哦,您的意思是像 sombody 将文件路径放入终端并且您想在之前给出一些静态符号或路径?像斜线 / 例如开头哪个 nocht 是可变的?
    • 并非如此。考虑到换行,我只想将 EditText 定位在 TextView 的最后一个字符之后。尝试过 RelativeLayout 但 EditText 只是离开屏幕。
    • 类似的问题,但是这家伙想在TextView的末尾添加一个按钮:stackoverflow.com/questions/17306961/…
    【解决方案3】:

    在您的代码中尝试此操作,根据您的要求为视图添加权重

       LinearLayout linearLayout=new LinearLayout(this);
       LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
       LayoutParams.WRAP_CONTENT);
       linearLayout.setLayoutParams(params);
       linearLayout.setWeightSum(1.0f);
       linearLayout.setOrientation(LinearLayout.HORIZONTAL);
       TextView textView=new TextView(this);
       EditText editText=new EditText(this);
       textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
       LayoutParams.WRAP_CONTENT,
       0.5f));
       editText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
       LayoutParams.WRAP_CONTENT,
       0.5f));
       linearLayout.addView(textView);
       linearLayout.addView(editText);
    

    【讨论】:

    • 感谢您的回答。不幸的是,EditText 没有跟随 TextView 中文本的结尾,因为更多的文本被异步添加。权重需要动态设置。我会添加更多细节。
    猜你喜欢
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多