与Toast消息的困境

时间:2017-11-16 16:23:21

标签: java android

我正在开发一个简单的应用程序,用户必须填写他们的名字,电子邮件,评价应用程序,并描述他们的体验(可选)。如果它们是空字段(名称和电子邮件),则会弹出错误消息。我在代码末尾遇到Toast.LENGTH_LONG()问题。它一直在说'方法调用预期'。

MainActivity.java

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

    Spinner spinner = (Spinner) findViewById(R.id.spinner1);

    //Create an array adapter
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.rate_array, android.R.layout.simple_spinner_dropdown_item);

    //Specify the layout for the drop down menu 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    //Apply the adapter to the spinner
    spinner.setAdapter(adapter);

    final EditText name = (EditText) findViewById(R.id.Name);
    final EditText e_mail = (EditText) findViewById(R.id.email);
    final Button submit = (Button) findViewById(R.id.submit);

    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(name.getText().length() == 0){
                name.setError("Please fill in your name");
            }
            else{
                if(e_mail.getText().length() == 0){
                    e_mail.setError("Please fill in your email");
                }
                else{
                    Toast.makeText(this, "Validation successful", 
                    Toast.LENGTH_LONG()).show();
                }
            }
        }
    });

 }
}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.infinite_space.myapplication.MainActivity"
android:orientation="vertical"
android:weightSum="1">

<TextView
    android:id="@+id/textView"
    android:layout_width="292dp"
    android:layout_height="37dp"
    android:text="Enter Feedback details to b sent to the developers"
    android:visibility="visible"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="16dp"
    android:layout_weight="0.18" />

<EditText
    android:id="@+id/Name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Your Name"
    android:inputType="textPersonName"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="67dp" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="fill_parent"
    android:layout_height="35dp"
    android:background="@android:drawable/btn_dropdown"
    android:spinnerMode="dropdown"
    tools:layout_editor_absoluteY="176dp"
    tools:layout_editor_absoluteX="16dp"
    android:prompt="@string/rate_level"
    android:entries="@array/rate_array" />

<EditText
    android:id="@+id/email"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Your email"
    android:inputType="textEmailAddress"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="122dp" />

<EditText
    android:id="@+id/details"
    android:layout_width="fill_parent"
    android:layout_height="103dp"
    android:ems="10"
    android:hint="Details..."
    android:inputType="text"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="227dp" />

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Would you like an email respond ?"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="343dp" />

<Button
    android:id="@+id/submit"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Send Feedback"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="385dp" />
</LinearLayout>

3 个答案:

答案 0 :(得分:2)

Toast.LENGTH_LONG是(常量)字段,而不是方法。将LENGTH_LONG()更改为LENGTH_LONG

答案 1 :(得分:0)

长时间LENGTH_LONG或短时间LENGTH_SHORT。摆脱LENGTH_LONG之后的()。

答案 2 :(得分:0)

试试这个:

 Toast.makeText(this, "Validation successful",Toast.LENGTH_LONG).show();