簡體   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