首页 > 解决方案 > 检查编辑文本是否为空

问题描述

我尝试了其他问题和答案,但没有找到解决方案。

我正在创建一个让人们搜索他人的社交应用程序。当我单击我想要的搜索栏并输入一个字母(例如 B)时,我会按预期看到名称为 B 的用户列表。但是当我删除 B 并且搜索栏为空时,仍然显示用户列表。

当搜索栏中没有任何内容时,我希望用户列表消失。这就是我在我的 activity_search 中创建编辑文本的方式:

<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="5dp"
android:hint="Search Username Here"
android:textSize="22dp"
android:id="@+id/input"
android:inputType="text"
android:layout_gravity="center"
android:background="@drawable/round_edit_text" />

这是我在 java 活动中设置编辑文本的地方:

input = findViewById(R.id.input);

这是整个java文件:

EditText input;
RecyclerView FindFriendsRecyclerList;
DatabaseReference mRef;
Query query;

Context mContext;
TextWatcher textWacher;

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

    input = findViewById(R.id.input);
    input.addTextChangedListener(textWacher);

    TextWatcher textWacher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String strText = s.toString();
            if (strText .length() == 0) {
                // removeList();
                FindFriendsRecyclerList.setVisibility(View.INVISIBLE);
                Toast.makeText(mContext, "Remove list", Toast.LENGTH_SHORT).show();
            } else {
                // showList();
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    FindFriendsRecyclerList = (RecyclerView) findViewById(R.id.recycler);
    FindFriendsRecyclerList.setLayoutManager(new LinearLayoutManager(this));

    findViewById(R.id.search).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            mRef = FirebaseDatabase.getInstance().getReference().child("Users");
            query = mRef.orderByChild("fullname").orderByChild("username").startAt(input.getText().toString()).endAt(input.getText().toString()+"\uf8ff");

            setdata();
        }
    });

    checkIfEmpty();

    if (input.getText().toString().length() == 0) {

        FindFriendsRecyclerList.setVisibility(View.INVISIBLE);
    }
}

private void setdata() {

    FirebaseRecyclerOptions<User> options =
            new FirebaseRecyclerOptions.Builder<User>()
                    .setQuery(query, User.class)
                    .build();

    FirebaseRecyclerAdapter<User, FindFriendsViewHolder> adapter =
            new FirebaseRecyclerAdapter<User, FindFriendsViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull FindFriendsViewHolder holder, final int position, @NonNull User model) {

                    holder.userName.setText(model.getUsername());
                    holder.userFullName.setText(model.getFullname());
                    Picasso.get().load(model.getImageurl()).placeholder(R.drawable.profile_pic).into(holder.profileImage);

                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            //String visit_user_id = getRef(position).getKey();

                            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                            fragmentTransaction.replace(R.id.search_users, new ProfileFragment()).commit();
                        }
                    });
                }

                @NonNull
                @Override
                public FindFriendsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

                    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.user_item, viewGroup, false);
                    FindFriendsViewHolder viewHolder = new FindFriendsViewHolder(view);
                    return viewHolder;
                }
            };

    FindFriendsRecyclerList.setAdapter(adapter);
    adapter.startListening();
}

public static class FindFriendsViewHolder extends RecyclerView.ViewHolder {

    public TextView userName, userFullName;
    public CircleImageView profileImage;

    public FindFriendsViewHolder(@NonNull View itemView) {
        super(itemView);

        userName = itemView.findViewById(R.id.username);
        userFullName = itemView.findViewById(R.id.fullname);
        profileImage = itemView.findViewById(R.id.image_profile);
    }
}

public void checkIfEmpty() {

    TextWatcher textWacher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String strText = s.toString();
            if (strText.length() == 0) {
                FindFriendsRecyclerList.setVisibility(View.INVISIBLE);
            } else {
                // showList();
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            FindFriendsRecyclerList.setVisibility(View.INVISIBLE);
        }
    };
}

正如您在上面看到的,我尝试了以下两种方法来检查它是否为空:

if (input.getText().toString().length() == 0) {

        FindFriendsRecyclerList.setVisibility(View.INVISIBLE);
    }

public void checkIfEmpty(View v){

    if(input.getText().toString().equals("")) {
        input.setVisibility(View.INVISIBLE);
    }
}

标签: javaandroidandroid-studioandroid-layout

解决方案


使用 TextWatcher 在 EditText 中获取文本状态。

例如,

public class MainActivity extends AppCompatActivity {

    private EditText input;

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

        input  = (EditText) findViewById(R.id.input);
        input.addTextChangedListener(textWacher);

    }

    TextWatcher textWacher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String strText = s.toString();
            if (strText .length() == 0) {
                // removeList();
                Toast.makeText(MainActivity.this, "remove", Toast.LENGTH_SHORT).show();
            } else {
                // showList();
                Toast.makeText(MainActivity.this, "show", Toast.LENGTH_SHORT).show();
            }


        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

}

推荐阅读