简体   繁体   English

在onActivityResult中显示吐司

[英]Show toast in onActivityResult

I am attempting to show a toast message when an activity returns with a result. 我试图在活动返回结果时显示吐司消息。 However, I am using a custom inflated toast. 但是,我正在使用定制的充气吐司。 Regular toast works fine, but my custom toast won't display. 普通吐司工作正常,但是我的自定义吐司不会显示。 Code below. 下面的代码。

Main Activity's onCreate() : 主要活动的onCreate()

@Override
    public void onCreate(Bundle savedInstance){
        super.onCreate(savedInstance);
        setContentView(R.layout.activity_login);

        emailTxt = (TextView)this.findViewById(R.id.email_field);
        passwordTxt = (TextView)this.findViewById(R.id.password_field);

        wgmService = ((MainApp)this.getApplication()).wegmannAdapter.create(WegmannService.class);

        spinner = (ProgressBar)this.findViewById(R.id.progress_bar);
        spinner.setVisibility(View.GONE);

        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast, (ViewGroup)findViewById(R.id.toast_layout));
        toastText = (TextView)layout.findViewById(R.id.toast_text);
        toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.BOTTOM, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);

        fadeIn = AnimationUtils.loadAnimation(this,  R.anim.fade_in);
        fadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out);
    }

Main Activity's onActivityResult : 主要活动的onActivityResult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        switch(requestCode) {
            case SIGNUP_VIEW:
                if(resultCode == RESULT_OK){
                    toastText.setText(data.getExtras().getString("message"));
                    toast.show();
                }
                break;
        }
    }

With the above, I see no toast message. 有了以上,我看不到吐司消息。 However, if I were to replace the lines in onActivityResult with Toast.makeText(getBaseContext(),"MESSAGE", Toast.LENGTH_LONG).show() , a message does appear. 但是,如果我用Toast.makeText(getBaseContext(),"MESSAGE", Toast.LENGTH_LONG).show()替换onActivityResult中的行Toast.makeText(getBaseContext(),"MESSAGE", Toast.LENGTH_LONG).show()出现一条消息。 I need to be able to use my custom toast in this instance. 在这种情况下,我需要能够使用我的自定义吐司。 Any help or direction appreciated. 任何帮助或指导表示赞赏。

Your error is quite simple: You change the text of the textField toastText and do not add text for the toast to show. 您的错误非常简单:您更改了textField toastText的文本,并且不添加要显示的Toast的文本。

This code should work: 此代码应工作:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    switch(requestCode) {
        case SIGNUP_VIEW:
            if(resultCode == RESULT_OK){
                toast.setText(data.getExtras().getString("message"));
                toast.show();
            }
            break;
    }
}

Turns out the issue is the line in my other activity. 原来问题出在我其他活动中。 I was setting my extra info with: 我正在通过以下方式设置我的额外信息:

Intent intent = new Intent();
            intent.putExtra("message", R.string.messages_signup_success);
            setResult(RESULT_OK, intent);
            finish();

However, R.string.messages_signup_success returns an int, which when retrieved throws a non-critical warning and proceeds without displaying my toast but without crashing. 但是, R.string.messages_signup_success返回一个int,当检索到该int时,它将引发非严重警告,并且继续进行而不会显示我的吐司但不会崩溃。 Altering the code to the below fixes the issue: 将代码更改为以下代码可解决此问题:

Intent intent = new Intent();
            intent.putExtra("message", getBaseContext().getString(R.string.messages_signup_success));
            setResult(RESULT_OK, intent);
            finish();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM