基于EditText输入为Android TextView实现textMultiline

时间:2015-12-09 08:26:21

标签: java android textview multiline

基本上,我有一个应用程序,用户在EditText字段中输入的内容是位图上显示为TextView中的文本。效果很好。您可以根据输入实时更新TextView文本。我只是不知道为什么在击球后它不会接受多条线路。我的代码有什么问题?

我已经包含以下重要的XML标记:

android:singleLine="false"
android:lines="20"
android:inputType="textMultiLine"

以下是我的EditText XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="0dp"
    android:orientation="vertical"
    android:id="@+id/layout">

    <EditText
        android:id="@+id/edtextDesigner"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_corner"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:typeface="sans"
        android:fontFamily="sans-serif-light"
        android:textSize="18sp"
        android:textColor="#000000"
        android:padding="10dp"
        android:alpha="0.5"
        android:singleLine="false"
        android:lines="20"
        android:inputType="textMultiLine"
        android:layout_below="@+id/toggleText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:visibility="gone" />

    <TextView
        android:id="@+id/textDesigner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:typeface="sans"
        android:text=""
        android:fontFamily="sans-serif-bold"
        android:textSize="48sp"
        android:textColor="#555555"
        android:visibility="gone"/>

</RelativeLayout>

以下是按下按钮后调用的功能;该函数打开EditText字段,其中包括:

public void CustomText(View view){
        if (tobt_Text.getVisibility() != View.VISIBLE){
            tobt_Text.setVisibility(View.VISIBLE);
            text_View.setVisibility(View.VISIBLE);
            edit_View.setOnKeyListener(new View.OnKeyListener() {

                // Sets max lines to 3
                @Override
                public boolean onKey(View view, int keyCode, KeyEvent event) {

                    if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {

                        if (((EditText) view).getLineCount() >= 3)
                            return true;
                    }

                    return false;
                }
            });
            tobt_Text.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    if (tobt_Text.isChecked()) {
                        edit_View.setVisibility(View.VISIBLE);
                        edit_View.bringToFront();
                    } else{
                        edit_View.setVisibility(View.GONE);
                        if (text_View.length() < 1)
                            tobt_Text.setVisibility(View.GONE);
                    }
                }
            });
            edit_View.addTextChangedListener(passwordWatcher);
            edit_View.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
            text_View.setOnTouchListener(new View.OnTouchListener() {
                int initialX = 0;
                int initialY = 0;

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getActionMasked()) {
                        case MotionEvent.ACTION_DOWN:
                            initialX = (int) event.getX();
                            initialY = (int) event.getY();
                            break;
                        case MotionEvent.ACTION_MOVE:
                            int currentX = (int) event.getX();
                            int currentY = (int) event.getY();
                            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) text_View.getLayoutParams();
                            int left = lp.leftMargin + (currentX - initialX);
                            int top = lp.topMargin + (currentY - initialY);
                            int right = lp.rightMargin - (currentX - initialX);
                            int bottom = lp.bottomMargin - (currentY - initialY);

                            lp.rightMargin = right;
                            lp.leftMargin = left;
                            lp.bottomMargin = bottom;
                            lp.topMargin = top;

                            text_View.setLayoutParams(lp);
                            break;
                        default:
                            break;
                    }
                    return true;
                }
            });
        }
    }

此功能根据输入更新文本:

    private final TextWatcher passwordWatcher = new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            text_View.setVisibility(View.VISIBLE);
        }

        public void afterTextChanged(Editable s) {
            if (s.length() == 0) {
                text_View.setVisibility(View.GONE);
            } else{
                text_View.setText(edit_View.getText());
            }
        }

    };

2 个答案:

答案 0 :(得分:1)

<EditText
    android:inputType="textMultiLine" <!-- Multiline input -->
    android:lines="8" <!-- Total Lines prior display -->
    android:minLines="6" <!-- Minimum lines -->
    android:gravity="top|left" <!-- Cursor Position -->
    android:maxLines="10" <!-- Maximum Lines -->
    android:layout_height="wrap_content" <!-- Height determined by content -->
    android:layout_width="fill_parent" <!-- Fill entire width -->
    android:scrollbars="vertical" <!-- Vertical Scroll Bar -->
/>

答案 1 :(得分:0)

答案是以编程方式设置具有以下特征的edit_View EditText字段:

    edit_View.setSingleLine(false);
    edit_View.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
    edit_View.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_MULTI_LINE);