简体   繁体   English

Toast消息不可见\\在Android中弹出

[英]Toast Message not visible\pop-up in android

I'm creating an android application in Java where IDE is Android Studio. 我正在用IDE为Android Studio的Java创建一个android应用程序。 This application gets two numeric input from the EditText and summation of those numbers will be shown in Toast Message on button click event. 此应用程序从EditText获取两个数字输入,这些数字的总和将在按钮单击事件上的Toast Message中显示。

Following is the XML code for reference (activity_main.xml) 以下是供参考的XML代码(activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"/>

        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"/>

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Sum" />
</LinearLayout>

Following is the Java code for the reference (MainActivity.java) 以下是参考的Java代码(MainActivity.java)

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Button;
import android.widget.Toast;
import android.content.Context;

public class MainActivity extends Activity {
private EditText ed1, ed2;
private Button b1;

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

    }

    public void addButtonListener(){
        ed1 = (EditText)findViewById(R.id.editText);
        ed2 = (EditText)findViewById(R.id.editText2);
        b1 = (Button)findViewById(R.id.button);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String value1 = ed1.getText().toString();
                String value2 = ed2.getText().toString();
                int a = Integer.parseInt(value1);
                int b = Integer.parseInt(value2);
                int sum = a + b;
                Context context = getApplicationContext();
                System.out.println(String.valueOf(sum));
                Toast toast = Toast.makeText(context,String.valueOf(sum),Toast.LENGTH_LONG);
                toast.show();
            }
        });
    }
}

I install this application on my Android Device for testing purpose. 我将此应用程序安装在Android设备上以进行测试。

My issue is that I don't get any Toast Message or Notification whenever I click on the Sum button while adding two values. 我的问题是,在添加两个值的同时单击“ 求和”按钮时,我不会收到任何Toast消息或通知。

I have also enabled the Notification in my Settings. 我还在“设置”中启用了“通知”。

在oncreate方法addButtonListener()中调用此方法

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Button;
import android.widget.Toast;
import android.content.Context;

public class MainActivity extends Activity {
private EditText ed1, ed2;
private Button b1;

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

}

public void addButtonListener(){
    ed1 = (EditText)findViewById(R.id.editText);
    ed2 = (EditText)findViewById(R.id.editText2);
    b1 = (Button)findViewById(R.id.button);

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String value1 = ed1.getText().toString();
            String value2 = ed2.getText().toString();
            int a = Integer.parseInt(value1);
            int b = Integer.parseInt(value2);
            int sum = a + b;
            Context context = getApplicationContext();
            System.out.println(String.valueOf(sum));
            Toast toast = Toast.makeText(context,String.valueOf(sum),Toast.LENGTH_LONG);
            toast.show();
            }
        });
    }
}

The reason that you are not seeing any Toast Message or Notification is that the click listener is inside addButtonListener() method. 您没有看到任何Toast消息或通知的原因是单击侦听器位于addButtonListener()方法内部。

All good so far but you forgot to call this method, so it will remain not used and the clickListener for your button will not be a created, so when you will press the button you will not see ant Toast Message or Notification. 到目前为止一切都很好,但是您忘记了调用此方法,因此它将保持不使用状态,并且不会创建按钮的clickListener,因此,当您按下按钮时,将不会看到ant Toast Message或Notification。

If you want to fix this just call this method inside onCreate / onStart() , for example: 如果要解决此问题,只需在onCreate / onStart()调用此方法,例如:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addButtonListener(); // now you will see your Toast
}

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

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