有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java禁用edittext 安卓,在视图中使用if-else

关于如何使用viewholder禁用EditText,或者如何使用if and else语句禁用EditText,请帮助我完成我的噩梦

我有一个回收视图,它列出了所有的名字和他们的消息。(可能是这样的)。不知何故,回收器视图由名称、消息和一个小标记textview“customer closed”组成,如果他们要单击由标记“customer closed”组成的回收器视图,他们将无法发送消息,因为它已经关闭。否则,由标记“customer closed”(客户已关闭)组成的所有recycler视图的EditText设置为false

 public void bind(final Account account, final FirebaseChat chat) {
    itemView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Context context = itemView.getContext();
            if (context instanceof NavigationActivity) {
                final Activity activity = (Activity) context;

                final Intent intent = new Intent(itemView.getContext(), myChat.class);
                intent.putExtra(ChatActivity.KEY_NEW, false);
                intent.putExtra(ChatActivity.KEY_ACCOUNT, account);
                intent.putExtra(ChatActivity.KEY_CHAT, chat);

                activity.startActivity(intent);
            }
        }
    });

    cName.setText(chat.getName());
    cName.setTypeface(chat.getReadCount() < chat.getNumMessages() ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
    cTextDate.setText(getFormattedDate(chat.getLastTime()));
    cTextMessage.setText(chat.getLastMessage());
    cTextMessage.setTypeface(chat.getReadCount() < chat.getNumMessages() ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);



}




public void bind(final Account account, final FirebaseChatInfo customerInfo) {
    itemView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Context context = itemView.getContext();
            if (context instanceof NavigationActivity) {
                final Activity activity = (Activity) context;
                final Intent intent = new Intent(itemView.getContext(), myChat.class);
                intent.putExtra(ChatActivity.KEY_NEW, false);
                intent.putExtra(ChatActivity.KEY_ACCOUNT, account);
                intent.putExtra(ChatActivity.KEY_CHAT_INFO, customerInfo);

                activity.startActivity(intent);
            }
        }
    });




    cName.setText(chatInfo.getName());
    cName.setTypeface(chatInfo.isLastVisitorMessaged() ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
    cTextDate.setText(getFormattedDate(chatInfo.getLastTime()));
    cTextMessage.setText(chatInfo.getLastMessage());
    cMessage.setTypeface(chatInfo.isLastVisitorMessaged() ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);

    this.shoptag = (TextView) itemView.findViewById(R.id.shoptag);
    shoptag.setText(chatInfo.getShop_id());
    this.customerclosed = (TextView) itemView.findViewById(R.id.customer_closed);
    this.customerclosed.setVisibility(chatInfo.isClosed() ? View.GONE : View.VISIBLE);
    this.message_editext = (EditText)itemView.findViewById(R.id.message_editext);

    message_text.setEnabled(!chatInfo.isClosed());

    if(customerInfo.isClosed())
    {

        message_text.setEnabled(false);


    }else {

        message_textt.setEnabled(true);
    }

}

我犯了这个错误

java.lang.NullPointerException: Attempt to invoke virtual method 'void 安卓.widget.EditText.setEnabled(boolean)' on a null object reference

共 (1) 个答案

  1. # 1 楼答案

    您所要做的就是添加else标记

        if(customerInfo.isClosed){
            message_editext.setEnabled(false);
        } else {
            message_editext.setEnabled(true);
        }