如何从EditText上的字符串测试启动DialogFragment?

时间:2015-05-05 01:52:36

标签: android android-edittext android-dialogfragment

如果用户按下Back按钮并且EditText行上有用户输入的输入,我希望从对话框片段中显示一个对话框。我不想在活动文件中自动启动dialogfragment。我想基于EditText文件中的字符串测试启动它(" BackButton.java")。

CardActivity.java文件:

public class CardActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cardviewinput);

    FragmentManager fm = getSupportFragmentManager();
    CreateFragment createFragment = new CreateFragment();
    createFragment.show(fm, "card_dialog");
...

BackButton.java文件:

public class OnBackCard extends EditText {

... 
}

public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) {
    this.keyCode = keyCode;
    this.event = event;
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        if (event.getAction() == KeyEvent.ACTION_DOWN
                && event.getRepeatCount() == 0) {

            getKeyDispatcherState().startTracking(event, this);
            return true;
        }

        else if (event.getAction() == KeyEvent.ACTION_UP) {
            getKeyDispatcherState().handleUpEvent(event);
            if (event.isTracking() && !event.isCanceled()) {
        final EditText ToDoEditText = (EditText) findViewById(R.id.CEditText);
        int stringToDo = ToDoEditText.getText().toString().trim().length();
        // Test the data input line for any user input data
     if(stringToDo>0) {
         // This is where I'm looking for code to launch the dialog and
         // the next lines close the soft keyboard 
         InputMethodManager imm = (InputMethodManager)
               context.getSystemService(Context.INPUT_METHOD_SERVICE);
         imm.hideSoftInputFromWindow(this.getWindowToken(), 0);
     return false;

CreateFragment.java文件:

public class CreateFragment extends DialogFragment {

public CreateFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.skyfrag_layout, container, false);
getDialog().setTitle("Delete card");
...
return rootView;
...

0 个答案:

没有答案