无法在alertDialog上点击按钮更改textView字符串

时间:2016-12-22 14:08:55

标签: android textview android-softkeyboard

我可以用textView.setText(“hello”)更改textView;在我的自定义适配器中的任何其他地方,除了我需要的地方,我在软键盘上点击完成。当点击完成按钮时,我添加了一个toast弹出窗口并且它可以工作,但我似乎无法从那里专门操作textView。请帮忙。

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;


class CustomAdapter extends ArrayAdapter{

    public CustomAdapter(Context context, ArrayList choreText) {
        super(context, R.layout.custon_listview_row, choreText);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater myInflater = LayoutInflater.from(getContext());
        View customView = myInflater.inflate(R.layout.custon_listview_row, parent, false);
        ImageButton imageButton = (ImageButton) customView.findViewById(R.id.imageButton_ID);
        final TextView textView = (TextView) customView.findViewById(R.id.textView_ID);
        final EditText input = new EditText(getContext());
        final AlertDialog OptionDialog = new AlertDialog.Builder(getContext()).create();



        //makes textView clickable
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //what happens when textView is clicked
                //final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                // put aler dialog box logic here
                OptionDialog.setTitle("Enter new chore");
                OptionDialog.setView(input);
                input.setInputType(InputType.TYPE_CLASS_TEXT);
                input.setImeOptions(EditorInfo.IME_ACTION_DONE);
                //checks if "Done" button on soft keyboard is clicked
                input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        if(actionId==EditorInfo.IME_ACTION_DONE){
                            //what happens when "Done" is clicked

                            //textView wont change to string hello
                            textView.setText("hello");

                            //this toast works
                            Toast.makeText(getContext(), "this is my Toast message!!! =)",
                                    Toast.LENGTH_LONG).show();
                            OptionDialog.dismiss();
                        }
                        return false;
                    }
                });



                OptionDialog.show();
            }
        });


        imageButton.setImageResource(R.drawable.clock);

        return customView;
    }
}

0 个答案:

没有答案