Toast 无法正常工作

Toast Not Working Properly

我在 Android Studio 中创建了一个应用程序;它工作正常,但 Toast 显示不正确。

感谢任何帮助。

这是我的代码:

MainActivity.java

public class MainActivity extends Activity {

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

    Button btn = (Button) findViewById(R.id.btn);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this, NextActivity.class);

            startActivity(intent);
        }
    });
}
}

activity_main.xml

<Button
    android:id="@+id/btn"
    android:text="click"
    android:layout_gravity="center_vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

NextActivity.java

public class NextActivity extends Activity {

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

    final EditText fName= (EditText) findViewById(R.id.txt);
    final EditText lName= (EditText) findViewById(R.id.txt1);
    Button btn1 = (Button) findViewById(R.id.btn1);

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(getApplicationContext(),"Hi there!, "+fName+ " " + " "+ lName, Toast.LENGTH_SHORT).show();
        }
    });

}

}

activity.next.xml

<EditText
    android:id="@+id/txt"
    android:hint="Enter your first name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginTop="110dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<EditText
    android:id="@+id/txt1"
    android:hint="Enter your last name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

<Button
    android:id="@+id/btn1"
    android:text="Submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txt1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="69dp" />

别忘了TextView.getText()

NextActivity.java

public class NextActivity extends Activity {

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

    final EditText fName= (EditText) findViewById(R.id.txt);
    final EditText lName= (EditText) findViewById(R.id.txt1);
    Button btn1 = (Button) findViewById(R.id.btn1);

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override 
        public void onClick(View v) {

            Toast.makeText(getApplicationContext(),"Hi there!, "+fName.getText().toString()+ " " + " "+ lName.getText().toString(), Toast.LENGTH_SHORT).show();
        } 
    }); 

} 

} 

我假设您想添加 EditText 的文本内容 - 您看到的字母和数字序列是默认 Object toString() 的结果:

public String toString ()

Added in API level 1 Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

getClass().getName() + '@' + Integer.toHexString(hashCode())

如果您想获取 EditText 小部件的内容,您可以 editText.getText().toString()