2017-08-03 55 views
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); 
    } 
} 

回答

0

從你的問題我不能告訴你如何檢測到改變的狀態。

你如何嘗試這種策略。

  1. 將onCreate中的editText的內容的狀態保存爲某個成員變量。
  2. 在返回按鈕按下時檢查editText的當前內容是否與您在onCreate上保存的成員變量相同。
  3. 如果它們不同,則使用該條件調用您的方法來顯示alertDialog。否則,讓用戶退出。使用上面的代碼

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); 
    } 
} 
+0

我以一個布爾變量來檢查用戶是否已經改變的EditText的狀態或not.Below是代碼 – Pritish

+0

能否請您發佈做同樣的事情你的代碼。我沒有足夠的業力來評論你最初的帖子要求。然後,我可以進一步提供幫助 – Stuckzilla

+0

我已經發布了上面的代碼 – Pritish