久久久久久久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問題)
主站蜘蛛池模板: 亚洲国产精品一区 | 欧美国产日韩一区二区三区 | 777777777亚洲妇女 | 久久久中文 | 中文字幕日韩专区 | 欧美全黄 | 岛国毛片 | 一区二区三区播放 | 亚洲欧美日本国产 | www.亚洲 | 日本网站免费观看 | 99色综合 | www4虎| 精品国产一区二区三区观看不卡 | 久久久久精 | 国产综合网站 | 四虎影院欧美 | 丁香久久 | 欧美一级片在线观看 | 91精品在线看 | 日韩激情在线 | 久久久www成人免费精品 | 久久精品一区 | 九九热国产精品视频 | 97人人爱| 欧美精品一区二区三区在线播放 | 欧洲成人午夜免费大片 | 夜夜爽99久久国产综合精品女不卡 | 国产清纯白嫩初高生视频在线观看 | 婷婷精品| 99久久久99久久国产片鸭王 | 亚洲午夜av久久乱码 | aaaaaa大片免费看最大的 | 毛片免费视频 | 欧美精品一区二区三区在线 | 中文字幕第7页 | 嫩草91在线| 羞羞的视频网站 | 国产综合在线视频 | 七七婷婷婷婷精品国产 | 一区二区久久电影 |