如何在alertDialog中使用runOnUiThread()来使用setText()方法更改我的textView

时间:2016-12-23 02:39:10

标签: android

我一直在尝试使用完成按钮点击软键盘,使用textView更改我的setText()文字。但它不会工作。我可以在其他任何地方设置文本,除了点击按钮。我也可以从完成按钮单击设置工作Toast,只是无法从内部操作textView。我被暗示我应该尝试runOnUiThread()方法,但我仍然无法让它工作,我花了好几个小时尝试。如果有任何不同,这都来自自定义适配器。

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
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();

        final MainActivity mn = new MainActivity();





        //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(input.getText().toString());
                            mn.runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    // your stuff to update the UI
                                    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;
    }
}

2 个答案:

答案 0 :(得分:0)

myactivity.runOnUiThread()错误,因为主要活动已经存在。将调用活动的引用传递给arrayadapter并调用{{1}}

答案 1 :(得分:0)

您的代码不在Activity的子类中,因此您不能使用runOnUiThread()方法,因为它是Activity的方法。

final MainActivity mn = new MainActivity();不正确,您不应该自己创建活动。

相反,您可以使用View.post(Runnable action),就像这样:

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.post(new Runnable() {
                @Override
                public void run() {
                    // your stuff to update the UI
                    textView.setText("hello");
                }
            });

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