我有自定义警告对话框。当我单击自定义对话框中的文本视图时,将关闭此对话框并打开另一个自定义对话框。但未关闭此自定义对话框。
Alert.Builder alertViewTaskDescription = new AlertDialog.Builder(
getActivity());
LayoutInflater inflaterViewTaskDescription = (LayoutInflater) getActivity()
.getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
viewTaskWithDescription = (View) inflaterViewTaskDescription
.inflate(R.layout.viewtaskwithdescriptionlayout, null,
false);
TextView textviewTask = (TextView) viewTaskWithDescription
.findViewById(R.id.taskidDaytoDayView);
TextView textviewDescription = (TextView) viewTaskWithDescription
.findViewById(R.id.descriptionidDaytoDayView);
textviewTask.setText(taskName);
textviewDescription.setText("ADD DESCRIPTION");
alertViewTaskDescription.setView(viewTaskWithDescription);
alertDialog = alertViewTaskDescription.create();
textviewDescription.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.cancel();
AlertDialog.Builder alertEditTaskDescription = new AlertDialog.Builder(getActivity());
LayoutInflater inflaterEditTaskDescription = (LayoutInflater) getActivity()
.getSystemService(
getActivity().LAYOUT_INFLATER_SERVICE);
View viewEditTaskDescription = (View) inflaterEditTaskDescription
.inflate(R.layout.edittaskwithdescriptionlayout,
null, false);
final EditText edittextTask = (EditText) viewEditTaskDescription
.findViewById(R.id.taskidDaytoDayEdit);
final EditText edittextDescription = (EditText) viewEditTaskDescription
.findViewById(R.id.descriptionidDaytoDayEdit);
edittextTask.setText(taskName);
edittextDescription.setText(taskDescription);
edittextTask.setSelection(edittextTask.getText()
.length());
edittextDescription
.setSelection(edittextDescription.getText()
.length());
alertEditTaskDescription
.setView(viewEditTaskDescription);
alertEditTaskDescription.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
}
});
alertViewTaskDescription.create().show();
}});
答案 0 :(得分:1)
我刚检查了以下代码:
public class MyActivity extends Activity {
private AlertDialog mDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AlertDialog.Builder alertViewTaskDescription = new AlertDialog.Builder(this);
LayoutInflater inflaterViewTaskDescription = (LayoutInflater) this
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final ViewGroup viewTaskWithDescription = (ViewGroup) inflaterViewTaskDescription
.inflate(R.layout.viewtaskwithdescriptionlayout, null, false);
TextView textviewTask = (TextView) viewTaskWithDescription
.findViewById(R.id.taskidDaytoDayView);
TextView textviewDescription = (TextView) viewTaskWithDescription
.findViewById(R.id.descriptionidDaytoDayView);
textviewTask.setText("Task Name");
textviewDescription.setText("ADD DESCRIPTION");
alertViewTaskDescription.setView(viewTaskWithDescription);
textviewDescription.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDialog().cancel();
createSecondDialog();
}});
mDialog = alertViewTaskDescription.create();
mDialog.show();
}
public AlertDialog getDialog() {
return mDialog;
}
private void createSecondDialog() {
AlertDialog.Builder alertEditTaskDescription = new AlertDialog.Builder(MyActivity.this);
LayoutInflater inflaterEditTaskDescription = (LayoutInflater) MyActivity.this
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View viewEditTaskDescription = (View) inflaterEditTaskDescription
.inflate(R.layout.edittaskwithdescriptionlayout,
null, false);
final EditText edittextTask = (EditText) viewEditTaskDescription
.findViewById(R.id.taskidDaytoDayEdit);
final EditText edittextDescription = (EditText) viewEditTaskDescription
.findViewById(R.id.descriptionidDaytoDayEdit);
edittextTask.setText("Task name 1");
edittextDescription.setText("Task description!");
edittextTask.setSelection(edittextTask.getText()
.length());
edittextDescription
.setSelection(edittextDescription.getText()
.length());
alertEditTaskDescription
.setView(viewEditTaskDescription);
alertEditTaskDescription.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
}
});
mDialog = alertEditTaskDescription.create();
mDialog.show();
}
}
它工作正常。我相信,在Activity中存储对话框解决了这个问题。请检查命名,因为看起来您正在调用错误的对话框以显示在onClick(View v)
- > alertViewTaskDescription.create().show();
(而不是alertEditTaskDescription.create().show()
)。