我试图在用户输入input后使用软键盘上的“Done”点击关闭alertDialog。我正在使用dismiss()方法,但它不起作用。我必须单击alertDialog框以避开它。此外,这是从自定义适配器类而非活动完成的。
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
builder.setTitle("Enter new chore");
builder.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.setText(input.getText().toString());
OptionDialog.dismiss();
}
return false;
}
});
builder.show();
}
});
imageButton.setImageResource(R.drawable.clock);
return customView;
}
}
答案 0 :(得分:0)
在onClick上试试这个:
//comment this line
//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());
AlertDialog OptionDialog = builder.create();
// put aler dialog box logic here
builder.setTitle("Enter new chore");
builder.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.setText(input.getText().toString());
OptionDialog.dismiss();
//send true as the event has occured
return true;
}
return false;
}
});
builder.show();
}
});