我一直在試圖從軟鍵盤上完成按鈕點擊更改使用setText()
我textView
文本改變我的TextView。但它不會工作。除了點擊按鈕之外,我可以在其他任何地方設置文本。我也可以從完成按鈕單擊設置工作Toast,只是不能從內部操作textView。我被暗示我應該嘗試runOnUiThread()
方法,但我仍然無法使其工作,我花了數小時的努力。如果它有所不同,這全部來自自定義適配器。如何使用runOnUiThread()在alertDialog使用setText()方法
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;
}
}
你'onEditorAction'已經在UI胎面。你爲什麼要使用runOnUiThread? –
我不確定自己是否只是來自其他堆棧溢出帖子的建議。 –