简体   繁体   中英

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

I have created my own custom keyboard using buttons. I was tired of Android's keyboard popping up and taking up half the screen. My keyboard is part of the Activity's layout. I have 2 EditTexts set to not-focusable to prevent the Android keyboard from popping up and make the EditTexts read-only. When I select buttons on my keyboard, the values of the buttons' text is displayed in the EditText field that I have assigned as focused (I do this using onTouch and setting a flag/boolean). If the flag mIsNameSelected = true , then the name EditText is considered focused, otherwise the email EditText is considered focused. Here's my dilemma;

First side-effect of this implementation is that when my text is longer than the space allotted to the EditText field, the text disappears to the right. I want to ellipsize the beginning as if the EditText was functioning normally but I think by setting it to isFocusable(false) I have eliminated it's ability to do this. Is there a work-around?

Second, to add a visual cue that the user has selected one of the EditTexts, I want to change the color of the underline on the EditText, not the text-link. The text link doesn't show up because the of the not-focusable attribute setting. I know there must be a way to change the EditText's style programmatically, which is what I'm looking for.

Here is a diagram:

在此处输入图片说明

The buttons call the type() method in XML

And here are the relevant methods (type() is called in XML):

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

        }
    }

Set

android:windowSoftInputMode="stateAlwaysHidden"

in the activity xml of your manifest. You can have your views as focusable without the keyboard popping up then.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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