0

这是我的片段的代码:

public class MainFragment extends Fragment {

private final AnimEffect forwardAnimation = AnimEffect.LEFT_TO_RIGHT;
private String couponNumber;
private EditText couponET;
@Override
public void onResume() {
    ((MainActivity) getActivity()).toggleBtn(false);
    super.onResume();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.mainfragment, container, false);
    ImageButton profile = (ImageButton) view.findViewById(R.id.Profile);
    ImageButton scan = (ImageButton) view.findViewById(R.id.Scan);
    Button validate = (Button) view.findViewById(R.id.Validate);
    couponET = (EditText) view.findViewById(R.id.couponET);

    OnClickListener profileListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            ((MainActivity) getActivity()).navigateTo(ProfileFragment.class, forwardAnimation);
        }
    };
    profile.setOnClickListener(profileListener);
    OnClickListener scanListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            ((MainActivity) getActivity()).navigateTo(ScannerFragment.class, forwardAnimation);
        }
    };
    scan.setOnClickListener(scanListener);

    OnClickListener validateListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            String response = couponET.getText().toString();
            if (response.contentEquals("ok")) {
                ((MainActivity) getActivity()).navigateTo(OKResponseFragment.class, forwardAnimation);
            }

        }
    };
    validate.setOnClickListener(validateListener);

    return view;
}

@Override
public void onStart() {
    super.onStart();
    if ((couponET != null) && (couponNumber != null)) {
        couponET.setText(couponNumber);
    }
}

public void setCouponNumber(String number) {
    couponNumber = number;
    if (couponET != null) {
        couponET.setText(number);
    }
}

public void alert() {
    final Dialog dialog = new Dialog(getActivity());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.warningdialog);
    dialog.setCancelable(true);
    Button ok = (Button) dialog.findViewById(R.id.cancel);
    ok.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    dialog.show();
}

}

现在,第一次,编辑文本完美运行,我可以使用它,可以在上面写字,任何东西。如果我改变片段,然后回来,我什么也做不了。如果我按下编辑文本,键盘不会出现。更重要的是,如果我不关闭键盘,在我更改片段之后,以便在我返回时打开键盘,如果我按下 EditText 然后在键盘上写字,什么也不会出现。

这是我的 FragmentActivity,如果有帮助的话:

public class MainActivity extends android.support.v4.app.FragmentActivity {

public static boolean scanner_active;

private Fragment f;

public static Intent login;

SharedPreferences settings;

private AnimEffect backPressAnimation = AnimEffect.LEFT_TO_RIGHT;

private ImageButton backButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    init();
    login = new Intent(MainActivity.this, WelcomeActivity.class);

}

private void init() {
    backButton = (ImageButton) findViewById(R.id.backButton);
    OnClickListener backButtonListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            Class<?> previousFragment = getPreviousScreen();
            if (previousFragment != null) {
                navigateTo(previousFragment, backPressAnimation);
            } else {
                finish();
                if(!Constants.remind){
                    startActivity(login);
                }
            }
        }
    };
    backButton.setOnClickListener(backButtonListener);
    String name;
    TextView nameTV = (TextView) findViewById(R.id.nameTV);
    if (Constants.remind) {
        settings = getSharedPreferences("PrefFile", 0);
        name = settings.getString("email", null);
    } else {
        name = Constants.email;
    }
    nameTV.setText(name);
    navigateTo(MainFragment.class, AnimEffect.FADE_IN);
}

@Override
public void onBackPressed() {
    Class<?> previousFragment = getPreviousScreen();
    if (previousFragment != null) {
        navigateTo(previousFragment, backPressAnimation);
    } else {
        finish();
        if(!Constants.remind){
            startActivity(login);
        }
    }
}

public void navigateTo(Class<?> fragmentClass, AnimEffect effect) {
    removeOldFragment(effect);
    int animIn = Util.getInAnimation(effect);
    try {
        f = (Fragment) fragmentClass.newInstance();
    } catch (Exception e) {
        LogService.err("navigate", e.getMessage(), e, LogService.LOG_LEVEL_LITE);
    }

    if (f != null) {
        getSupportFragmentManager().beginTransaction().disallowAddToBackStack().setCustomAnimations(animIn, 0).add(R.id.fragment, f, fragmentClass.getSimpleName()).commitAllowingStateLoss();
    }
}

public Class<?> getPreviousScreen() {
    if ((f instanceof MainFragment) && f.isAdded()) {
        return null;
    } else if ((f instanceof ProfileFragment) && f.isAdded()) {
        backPressAnimation = AnimEffect.LEFT_TO_RIGHT;
        return MainFragment.class;
    } else if ((f instanceof HelpFragment) && f.isAdded()) {
        backPressAnimation = AnimEffect.LEFT_TO_RIGHT;
        return MainFragment.class;
    } else if ((f instanceof ScannerFragment) && f.isAdded()) {
        backPressAnimation = AnimEffect.LEFT_TO_RIGHT;
        return MainFragment.class;
    } else if ((f instanceof OKUserResponseFragment) && f.isAdded()) {
        backPressAnimation = AnimEffect.LEFT_TO_RIGHT;
        return MainFragment.class;
    } else if ((f instanceof OKResponseFragment) && f.isAdded()) {
        backPressAnimation = AnimEffect.LEFT_TO_RIGHT;
        return MainFragment.class;
    }
    return null;
}

private void removeOldFragment(AnimEffect effect) {
    if ((f != null) && f.isAdded()) {
        int animOut = Util.getOutAnimation(effect);
        getSupportFragmentManager().beginTransaction().setCustomAnimations(0, animOut).remove(f).commitAllowingStateLoss();
        getSupportFragmentManager().executePendingTransactions();
    }
}

public void toggleBtn(boolean b) {
    backButton.setEnabled(b);
    if(b){
        backButton.setImageResource(R.drawable.back);
    }else{
        backButton.setImageResource(R.drawable.besmart2);
    }

}

public void setCouponText(String text) {
    // TODO Auto-generated method stub
    navigateTo(MainFragment.class, backPressAnimation);
    if ((f != null) && (f instanceof MainFragment)) {
        ((MainFragment) f).setCouponNumber(text);
    }
}

}

编辑:它适用于 4.x,但不适用于以前的操作系统,如 2.3.5 或 3.2

4

1 回答 1

0

我看到如果我按下验证按钮,如果编辑文本不起作用,则会出现一个警报,然后如果我关闭警报,它就会起作用。所以我所做的是,将一个布尔参数传递给警报,以便如果为真,则调用 dialog.dismiss();。我调用我的 onCreateView() 函数:

alert(true);

这会创建一个警报,并快速解除它,而不显示它,使编辑文本工作

于 2012-11-28T13:24:10.750 回答