android:inflate Toast在2.3.3上没有获得clickEvents

时间:2012-05-16 08:40:50

标签: android android-layout

以下代码适用于ICS,而不适用于2.3.3。

我使用带CheckBox的自定义Toast,并在ICS上触发clickEvents。

我是否必须创建自定义对话框并将其称为活动或我可以解决它?

public void showToastwithImage(String text, int imageID, boolean forceShow) {

    if (prefs.getBoolean("prefs_showHelp", true)) {
        // create the view
        View view = inflateView(R.layout.message_panel);

        // set the image in the view
        ImageView im = (ImageView) view.findViewById(R.id.panel_icon);
        im.setImageResource(imageID);

        // set the text in the view
        TextView tv = (TextView) view.findViewById(R.id.message);
        tv.setText(text);

        CheckBox cb = (CheckBox) view.findViewById(R.id.checkBox1);

        if(forceShow){
            cb.setVisibility(View.GONE);
        }else{
            cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putBoolean("prefs_showHelp", false);
                    editor.commit();
                }
            });
        }


        // show the toast
        Toast toast = new Toast(this);
        toast.setView(view);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
    }

}
private View inflateView(int resource) {
    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    return vi.inflate(resource, null);
}

2 个答案:

答案 0 :(得分:1)

custom dialog is better than toast...

您可以在自定义对话框中轻松设置视图....并且toast用于Flash消息而无需交互。

Dialog dialog = new Dialog(YourActivityName.this);
dialog.setContentView(R.layout.dialoglayout);

ImageView im = (ImageView) dialog.findViewById(R.id.panel_icon);
TextView tv = (TextView) dialog.findViewById(R.id.message);
CheckBox cb = (CheckBox) dialog.findViewById(R.id.checkBox1);

dialog.show();

答案 1 :(得分:1)

Toast应该用于显示短消息而不提供任何用户交互,因为它们在2或3.5秒的持续时间内可见。因此,我建议您使用Dialog或Activity(带对话框主题)供用户与CheckBox进行交互。