0
我有一個editext,如果我輸入一些文本,並單擊後退按鈕,它會顯示一個對話框詢問我是否放棄更改或不是,但我想顯示鍵盤上創建的活動,所以我添加了按照我的片段AlertDialog的怪異行爲
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
現在,當我對我做的EditText變化AlertDialog說你是否要放棄更改顯示不出來的oncreateview線?
如果我刪除線
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);
}
}
我以一個布爾變量來檢查用戶是否已經改變的EditText的狀態或not.Below是代碼 – Pritish
能否請您發佈做同樣的事情你的代碼。我沒有足夠的業力來評論你最初的帖子要求。然後,我可以進一步提供幫助 – Stuckzilla
我已經發布了上面的代碼 – Pritish