【问题标题】:RecyclerView EditText - check if it has value or is nullRecyclerView EditText - 检查它是否有值或为空
【发布时间】:2018-07-10 03:05:56
【问题描述】:

我目前在RecyclerView 的每个项目中都有 4 个EditTexts。

场景是:用户只填写了 item(n) 上的 EditTexts 的 2/4,然后继续进行 item(n),填写了 item(n) 上的所有内容并点击了提交按钮.

应该有一个消息对话框,其中有一个尚未填充的EditText

我怎么知道还有一个剩余的EditText 还没有被填满每个项目?

@Override
public void onBindViewHolder(final CreditCard_PostPayAdapter.MyViewHolder holder,int position) {
    final int pos = position;
    //final AccountDetailsItem accountDetailsItem = accountDetailsItemList.get(position);   

    button.setOnClickNewListener(new onClickListener)
    {
        //Checks if edittext1,edittext2,editext3,edittext4 of item(n)/item(pos) have value or null.
        //if all edittext in item(n) have values then proceed to the next item.
    }
}

public class MyViewHolder extends RecyclerView.ViewHolder {

    public ediText editText1,editText2,editText3,editText4
    public ImageButton mDeleteButton;
    public MyViewHolder(View view) {
        super(view);
        this.editText1 = view.findViewByiId(myedittext1);
           this.editText2 = view.findViewByiId(myedittext2);
              this.editText3= view.findViewByiId(myedittext3);
                 this.editText4 = view.findViewByiId(myedittext4);
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_overflow"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/item_ripple"
    android:clickable="true"
    android:focusable="true"
    android:foregroundGravity="center_horizontal"
    android:orientation="vertical"
    >

    <ScrollView
        android:layout_width="match_parent"
        android:fillViewport="true"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="wrap_content">

            <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="horizontal"
                android:background="#ffffff"
                android:layout_width="match_parent"
                android:layout_height="50dp">

                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:layout_marginLeft="8dp"
                    android:id="@+id/mDeleteButton"
                    android:background="@android:color/transparent"
                    android:src="@drawable/btn_delete_documentpic"/>
                <TextView
                    android:id="@+id/header_title"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:layout_marginTop="15dp"
                    android:text="@string/lorem_ipsum_short"
                    android:textColor="@color/aub_red_new"
                    android:textSize="20sp"
                    android:layout_marginLeft="4dp"

                    android:textStyle="bold" />

                <ImageView
                    android:layout_weight=".2"
                    android:src="@drawable/ic_up_black"
                    android:layout_width="0dp"
                    android:padding="12dp"
                    android:layout_height="50dp"
                    android:id="@+id/arrow_indicator"/>
            </LinearLayout>
            <View
                android:id="@+id/mHeaderDivider"
                android:layout_width="match_parent"
                android:layout_height=".8dp"
                android:alpha="0.8"
                android:layout_marginBottom="16dp"
                android:background="#CACACA"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/mBodyLinear"
                android:orientation="vertical">

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="16dp"
                    android:layout_marginTop="16dp"
                    app:counterTextAppearance="@style/counterText"
                    app:counterOverflowTextAppearance="@style/counterOverride"
                    app:theme="@style/TextInputLayoutGold">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/mMerchantNameTextView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:singleLine="true"
                        android:textSize="@dimen/font_size"
                        android:inputType ="numberDecimal"
                        app:theme="@style/EditTextGold"
                        android:hint="@string/label_merchant_name"
                        />
                </android.support.design.widget.TextInputLayout>
                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="16dp"
                    app:counterTextAppearance="@style/counterText"
                    app:counterOverflowTextAppearance="@style/counterOverride"
                    app:theme="@style/TextInputLayoutGold">

                    <android.support.design.widget.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:singleLine="true"
                        android:textSize="@dimen/font_size"
                        android:inputType ="numberDecimal"
                        app:theme="@style/EditTextGold"
                        android:hint="@string/hint_transaction_amount"
                        />
                </android.support.design.widget.TextInputLayout>

     <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="16dp"
                    android:layout_marginTop="16dp"
                    app:counterTextAppearance="@style/counterText"
                    app:counterOverflowTextAppearance="@style/counterOverride"
                    app:theme="@style/TextInputLayoutGold">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/mMerchantNameTextView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:singleLine="true"
                        android:textSize="@dimen/font_size"
                        android:inputType ="numberDecimal"
                        app:theme="@style/EditTextGold"
                        android:hint="@string/label_merchant_name"
                        />
                </android.support.design.widget.TextInputLayout>
                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="16dp"
                    app:counterTextAppearance="@style/counterText"
                    app:counterOverflowTextAppearance="@style/counterOverride"
                    app:theme="@style/TextInputLayoutGold">

                    <android.support.design.widget.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:singleLine="true"
                        android:textSize="@dimen/font_size"
                        android:inputType ="numberDecimal"
                        app:theme="@style/EditTextGold"
                        android:hint="@string/hint_transaction_amount"
                        />
                </android.support.design.widget.TextInputLayout>

            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

【问题讨论】:

  • 也许可以添加一些代码来显示您尝试过的内容。
  • 我已经有适配器,我没有任何代码。
  • 只需检查您的每个EditTexts,看看单击按钮时它们是否为空。

标签: android android-recyclerview


【解决方案1】:

我不知道你在寻找什么,但你可以这样做:

String editTextStr = editText.getText().toString();

if (editTextStr.equals(""))
{
    // editText is empty
}

检查您的每个EditTexts 是否为空。

如果您想要 boolean 的值,如果 EditText 为空,您可以尝试:

boolean isEditTextEmpty = editText.getText().toString().isEmpty();

【讨论】:

    【解决方案2】:

    我认为 TextWatcher 可以在这里完成这项工作。它会在 EditText 上的每个键击中触发。从那里,您可以检查是否已填充其他 EditText 并采取相应措施。请注意,侦听器会在每次击键时触发,您需要正确处理该操作。

    这里的这篇文章给你一些类似的想法。

    myTextBox.addTextChangedListener(new TextWatcher() {  
    
        public void afterTextChanged(Editable s) {}  
    
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 
    
        public void onTextChanged(CharSequence s, int start, int before, int count) {  
        //your code here
        }
    });
    

    android edittext onchange listener

    【讨论】:

    • 这无济于事,因为我需要检查提交按钮中的所有内容,而不是使用 textwatcher
    • 请分享更多信息,我看了代码,button.setOnClick的按钮是在哪里创建的?
    • 如果可能,发布完整的适配器和 xml 文件以便更好地理解。
    • 我仍然无法分辨这个按钮的按钮创建。setOnClickNewListener(new onClickListener)。我的猜测是,这个按钮来自活动,而不是适配器,即不是来自 MyViewHolder,类和 xml 没有它。如果是这种情况(按钮是在活动中创建的,那么很可能并且实际上很常见通过在活动上使用回调而不是适配器本身来处理所有交互。
    猜你喜欢
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多