如何将EditText附加到TextView的末尾

时间:2015-01-15 11:27:25

标签: android terminal console android-edittext textview

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

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

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

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

我会尽力澄清我想要完成的事情。

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

我想要的是什么:

example

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

为了避免在不同的屏幕尺寸上,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>

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

答案 2 :(得分:0)

通过在EditText(etShell)上的每次追加后更新TextView(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);
相关问题