久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

編輯文本字段為空時禁用按鈕

Disable Button when Edit Text Fields empty(編輯文本字段為空時禁用按鈕)
本文介紹了編輯文本字段為空時禁用按鈕的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有兩個文本字段和一個按鈕.我想禁用按鈕,除非兩個 EditText-Fields 都不為空.我在stackoverflow上嘗試了許多解決方案,但它們不起作用.這是我的代碼:

I have two text fields and a Button. I want to disable the Button unless both EditText-Fields are not empty. I tried many solutions here at stackoverflow, but they don't work. Here is my code:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class RegisterActivity extends Activity {

    private EditText editText1;
    private EditText editText2;

    //TextWatcher
    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3)
       {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            checkFieldsForEmptyValues();
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    };


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


        editText1 = (EditText) findViewById(R.id.reg_password);
        editText2 = (EditText) findViewById(R.id.reg_password2);

        //set listeners
        editText1.addTextChangedListener(textWatcher);
        editText1.addTextChangedListener(textWatcher);

        // run once to disable if empty
        checkFieldsForEmptyValues();


        TextView loginScreen = (TextView) findViewById(R.id.link_to_login);
        loginScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity (new Intent(getApplicationContext(), DialogActivity.class));
                finish();
            }
        });

    }

    private  void checkFieldsForEmptyValues(){
        Button b = (Button) findViewById(R.id.btnRegister);

        String s1 = editText1.getText().toString();
        String s2 = editText2.getText().toString();

        if(s1.equals("") && s2.equals(""))
        {
            b.setEnabled(false);
        }

        else if(!s1.equals("")&&s2.equals("")){
            b.setEnabled(false);
        }

        else if(!s2.equals("")&&s1.equals(""))
        {
            b.setEnabled(false);
        }

        else
        {
            b.setEnabled(true);
        }
    }
}

如果我開始活動,它會被禁用.但是如果我輸入一些東西,它有時會啟用,有時不會.如果兩個編輯文本字段都不為空,我只想啟用按鈕.

If I start the activity, it is disabled. But If I type something, it sometimes enables and sometimes doesn't.. I just want to enable the button if both Edit-Text Fields are not empty.

推薦答案

你的問題在這里:

//set listeners
        editText1.addTextChangedListener(textWatcher);
        editText1.addTextChangedListener(textWatcher);

您沒有將 textWatcher 設置為 editText2,因此如果您在 editText1 中寫入,則始終檢查條件

You are not setting the textWatcher to editText2, so you are always checking the condition if you write inside editText1

這篇關于編輯文本字段為空時禁用按鈕的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cut, copy, paste in android(在android中剪切、復制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數的數字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 亚洲精品中文字幕在线观看 | 国产精品三级久久久久久电影 | 国产高清精品一区二区三区 | 日韩欧美在线观看 | 男人av在线播放 | 午夜羞羞| 国产一级免费在线观看 | 玖操| 国产精品成人一区二区三区夜夜夜 | 亚洲精品视频一区二区三区 | 日本精品久久 | 人人种亚洲 | 国产午夜三级一区二区三 | 成人小视频在线免费观看 | 久久精品视频亚洲 | 九九九视频 | 777zyz色资源站在线观看 | 国产精品久久一区 | 国产美女精品视频 | 成人免费淫片aa视频免费 | 激情五月婷婷在线 | 欧美影院| 亚洲精品久久久久久久久久久 | 看羞羞视频免费 | 免费一级黄色录像 | 一区二区三区免费在线观看 | 国产va| 精品亚洲国产成av人片传媒 | 久久久久久久久久久高潮一区二区 | 午夜影院在线观看免费 | 精品亚洲国产成av人片传媒 | 精品在线视频播放 | 国产精品伦一区二区三级视频 | 久久久精彩视频 | 国产日韩欧美激情 | 成人一级片在线观看 | 国产精品自产av一区二区三区 | 在线观看www | 日韩欧美二区 | 欧美黄 片免费观看 | 美女爽到呻吟久久久久 |