在键盘上方添加textview [英] Add textview right above Keyboard

查看:66
本文介绍了在键盘上方添加textview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我的应用由一个位于屏幕底部的 EditText 和一个按钮组成.如果我在 EditText 中输入内容,然后点击该按钮,则会在屏幕顶部创建 TextView .

At the moment my app consists from a EditText and a button, both at the bottom of the screen. If I'm typing something into the EditText and than hit that button a TextView gets created at the top of the screen.

但是,我想要实现的是,如果我按下该按钮,则会在 EditText 字段上方创建 TextView .如果再次执行此操作,我想再次在 EditText 上方但在另一个 TextView 下方创建第二个 TextView .

But, what I want to achieve is that if I hit that button, that the TextView gets created right above the EditText field. And if I do that again I want the second TextView again created right above the EditText but below the other TextView.

到目前为止,这是我的代码.

This is my code so far.

这是xml :(所有内容都在 RelativeLayout 内部.但是由于某些原因,由于不能将其检测为代码,所以我无法在其中放置它).

Here's the xml: (This is all inside a RelativeLayout. But for some reason I can't put that in here because its not detected as code...)

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/linearLayout"
    android:orientation="vertical" >
</LinearLayout>


<Button
    android:id="@+id/button"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:text="Senden"
    android:layout_gravity="center_horizontal"
    android:layout_alignBottom="@+id/editText"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<EditText
    android:layout_width="470px"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

我的活动:

public class MainActivity extends AppCompatActivity {

private Button button;
private EditText editText;
private LinearLayout linearLayout;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
    editText = (EditText) findViewById(R.id.editText);
    button = (Button) findViewById(R.id.button);

    final View.OnClickListener btnHandler = new View.OnClickListener() {
        public void onClick(View v) {
            String msg = editText.getText().toString();
            editText.getText().clear();
            linearLayout.addView(createNewTextView(msg));
            View view = getCurrentFocus();
            if(view != null) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        }
    };

    button.setOnClickListener(btnHandler);
}

private TextView createNewTextView(String text) {
    final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    final TextView textView = new TextView(this);
    textView.setLayoutParams(lparams);
    textView.setTextColor(Color.BLACK);
    textView.setText(text);

    return textView;
}

我需要更改什么以使其看起来像我想要的那样?

What do I need to change so that it appears like i want it to?

推荐答案

使用 LinearLayout 作为父视图, android:gravity ="center | bottom" 此属性.

Use LinearLayout as parent view with android:gravity="center|bottom" this property.

现在,在添加文本视图时,插入带有位置的新创建的文本视图.

Now while adding textviews, insert newly created text view with position.

parentLinearLayout.addView(childtextview, index); //index represents it position in linearlayout,according to you index is always 2.

这篇关于在键盘上方添加textview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆