我有一个editext,如果我输入一些文本并单击后退按钮,它会显示一个对话框,询问我是否放弃更改但是我想在创建活动时显示键盘,所以我在我的中添加了以下行片段的oncreateview
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
现在当我在我的edittext中进行更改时,AlertDialog说你是否要放弃更改并不会显示?
如果我删除该行
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
从我的onCreateView它显示AlertDialog。
public class EditorFragment extends Fragment implements android.support.v4.app.LoaderManager.LoaderCallbacks<Cursor>, EditorActivity.OnBackPressedListener {
private EditText TaskName, DueDate, DueTime;
private boolean taskChanged = false;
private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
taskChanged = true;
return false;
}
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_editor, container, false);
TaskName = (EditText) view.findViewById(R.id.tasknameedittext);
DueDate = (EditText) view.findViewById(R.id.duedateedittext);
DueTime = (EditText) view.findViewById(R.id.duetimeedittext);
TaskName.setOnTouchListener(onTouchListener);
DueDate.setOnTouchListener(onTouchListener);
DueTime.setOnTouchListener(onTouchListener);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getActivity().getMenuInflater().inflate(R.menu.menu_editor, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.editoractivitydelete:
showDeleteConfirmationDialog();
return true;
case R.id.editoractivitysave:
saveTask();
return true;
case android.R.id.home:
//up button code
if (!taskChanged) {
NavUtils.navigateUpFromSameTask(getActivity());
return true;
}
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
NavUtils.navigateUpFromSameTask(getActivity());
}
};
showUnsavedChangesDialog(onClickListener);
return true;
}
return super.onOptionsItemSelected(item);
}
private void showUnsavedChangesDialog(DialogInterface.OnClickListener onClickListener) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.discardchanges);
builder.setPositiveButton(R.string.yes, onClickListener);
builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (dialog != null) {
dialog.dismiss();
}
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((EditorActivity) getActivity()).setOnBackPressedListener(this);
}
//Back Button code
@Override
public void doBack() {
if (!taskChanged) {
NavUtils.navigateUpFromSameTask(getActivity());
return;
}
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
NavUtils.navigateUpFromSameTask(getActivity());
}
};
showUnsavedChangesDialog(onClickListener);
}
}
答案 0 :(得分:0)
我无法从你的问题中看出你是如何发现变化状态的。
你尝试这个策略怎么样。
使用上述代码的示例
public class EditorFragment extends Fragment implements android.support.v4.app.LoaderManager.LoaderCallbacks<Cursor>, EditorActivity.OnBackPressedListener {
private EditText TaskName, DueDate, DueTime;
private String originalTask = "";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_editor, container, false);
TaskName = (EditText) view.findViewById(R.id.tasknameedittext);
DueDate = (EditText) view.findViewById(R.id.duedateedittext);
DueTime = (EditText) view.findViewById(R.id.duetimeedittext);
originalTask = TaskName.getText().toString();
TaskName.setOnTouchListener(onTouchListener);
DueDate.setOnTouchListener(onTouchListener);
DueTime.setOnTouchListener(onTouchListener);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getActivity().getMenuInflater().inflate(R.menu.menu_editor, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.editoractivitydelete:
showDeleteConfirmationDialog();
return true;
case R.id.editoractivitysave:
saveTask();
return true;
case android.R.id.home:
//up button code
if (!taskChanged) {
NavUtils.navigateUpFromSameTask(getActivity());
return true;
}
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
NavUtils.navigateUpFromSameTask(getActivity());
}
};
showUnsavedChangesDialog(onClickListener);
return true;
}
return super.onOptionsItemSelected(item);
}
private void showUnsavedChangesDialog(DialogInterface.OnClickListener onClickListener) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.discardchanges);
builder.setPositiveButton(R.string.yes, onClickListener);
builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (dialog != null) {
dialog.dismiss();
}
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((EditorActivity) getActivity()).setOnBackPressedListener(this);
}
//Back Button code
@Override
public void doBack() {
if (!originalTask.equals(TaskName.getText().toString()))
{
NavUtils.navigateUpFromSameTask(getActivity());
return;
}
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
NavUtils.navigateUpFromSameTask(getActivity());
}
};
showUnsavedChangesDialog(onClickListener);
}
}