繁体   English   中英

更改EditText的下划线/样式…而不是链接,然后水平滚动文本

[英]Change the underline/style of EditText…not the link, and scroll the text horizontally

我使用按钮创建了自己的自定义键盘。 我已经厌倦了Android的键盘弹出并占据了一半的屏幕。 我的键盘是活动布局的一部分。 我将2个EditTexts设置为不可聚焦,以防止Android键盘弹出并使EditTexts为只读。 当我选择键盘上的按钮时,按钮文本的值将显示在已分配为焦点的EditText字段中(我使用onTouch并设置了一个标志/布尔值来执行此操作)。 如果标志mIsNameSelected = true ,则名称EditText被认为是关注的,否则电子邮件EditText被认为是关注的。 这是我的困境;

此实现的第一个副作用是,当我的文本长于分配给EditText字段的空间时,文本将消失在右侧。 我想省略开头,就像EditText正常运行一样,但是我认为通过将其设置为isFocusable(false)我已经消除了它执行此操作的能力。 有解决方法吗?

其次,要添加一个视觉提示,表明用户选择了一个EditText,我想更改EditText而不是文本链接上的下划线的颜色。 由于无法聚焦的属性设置,该文本链接未显示。 我知道必须找到一种以编程方式更改EditText样式的方法,这就是我要寻找的。

这是一个图:

在此处输入图片说明

按钮调用XML中的type()方法

这里是相关的方法(在XML中调用type()):

public void setActivteListener(final EditText et) {
        et.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int result = event.getAction();
                if (result == MotionEvent.ACTION_DOWN) {
                    if (et.getId() == R.id.et_customer_name) {
                        Log.d(TAG, "here on customer name");
                        mIsNameSelected = true;
                        updateUI();
                    } else if (et.getId() == R.id.et_customer_email) {
                        Log.d(TAG, "here on customer email");
                        mIsNameSelected = false;
                        updateUI();
                    }
                }
                return true;
            }
        });
    }

    public void type(View view) {

        Log.d(TAG, "type method called");

        Button btn = (Button) view;
        String txt = btn.getText().toString();
        char value = txt.charAt(0);

        if (mIsNameSelected) {


            if (mCustomerName.getText().length() > 0) {

                if (txt.equalsIgnoreCase("back")) {

                    char[] text = mCustomerName.getText().toString().toCharArray();
                    char[] temp = Arrays.copyOfRange(text, 0, text.length - 1);
                    mCustomerName.setText(String.valueOf(temp));
                }
            }

            if (txt.equalsIgnoreCase("space")) {
                char[] text = mCustomerName.getText().toString().toCharArray();

                Log.d(TAG, "the value of text now : " + String.valueOf(text));

                char[] temp = Arrays.copyOf(text, text.length + 1);

                Log.d(TAG, "the value of temp now: " + String.valueOf(temp));

                temp[temp.length - 1] = ' ';

                Log.d(TAG, "temp to string: " + String.valueOf(temp));

                mCustomerName.setText(String.valueOf(temp));
            }

            if (!(txt.equalsIgnoreCase("back") || txt.equalsIgnoreCase("space"))) {
                char[] text = mCustomerName.getText().toString().toCharArray();
                char[] temp = Arrays.copyOf(text, text.length + 1);
                temp[temp.length - 1] = value;
                mCustomerName.setText(String.valueOf(temp));
            }

        } else {
            Log.d(TAG, "the length of the et field is : " + mCustomerName.getText().length());

            if (mCustomerEmail.getText().length() > 0) {

                Log.d(TAG, "the text is longer than 0");

                if (txt.equalsIgnoreCase("back")) {

                    Log.d(TAG, "the text = back ");

                    char[] text = mCustomerEmail.getText().toString().toCharArray();
                    char[] temp = Arrays.copyOfRange(text, 0, text.length - 1);
                    mCustomerEmail.setText(String.valueOf(temp));
                }
            }

            if (txt.equalsIgnoreCase("space")) {
                char[] text = mCustomerEmail.getText().toString().toCharArray();

                Log.d(TAG, "the value of text now : " + String.valueOf(text));

                char[] temp = Arrays.copyOf(text, text.length + 1);

                Log.d(TAG, "the value of temp now: " + String.valueOf(temp));

                temp[temp.length - 1] = ' ';

                Log.d(TAG, "temp to string: " + String.valueOf(temp));

                mCustomerEmail.setText(String.valueOf(temp));
            }

            if (!(txt.equalsIgnoreCase("back") || txt.equalsIgnoreCase("space"))) {
                char[] text = mCustomerEmail.getText().toString().toCharArray();
                char[] temp = Arrays.copyOf(text, text.length + 1);
                temp[temp.length - 1] = value;
                mCustomerEmail.setText(String.valueOf(temp));
            }

        }
    }

android:windowSoftInputMode="stateAlwaysHidden"

在清单的活动xml中。 您可以在不弹出键盘的情况下使视图具有焦点。

https://developer.android.com/guide/topics/manifest/activity-element.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM