我想弄清楚为什么每当按下按钮时我的toast消息都没有弹出。无论何时按下按钮,都没有任何反应。我做了一些研究,无法弄清楚这一点。我对此很新。我是一名学生,正在努力学习如何成为一名Android开发人员。有人可以帮忙吗?
这是我的java:
package com.example.mymusicplaylist;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class NationActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nation);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void nClickHandler(View v) {
Button nBut = (Button) findViewById(R.id.nationButton);
nBut.isClickable();
switch (v.getId()) {
case R.id.nationButton:
if (nBut.isPressed()) {
Toast nToast = Toast.makeText(this, "Just imagine listening to the song", Toast.LENGTH_LONG);
nToast.show();
}
}
}
}
And here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true"
tools:context="com.example.mymusicplaylist.NationActivity"
android:background="@drawable/nation"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_nation" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="@string/nation"
android:layout_gravity="center"
android:textStyle="bold"
android:textColor="@android:color/white"
android:textSize="22sp"
android:layout_weight="1"
android:layout_marginTop="62dp"/></LinearLayout>
<Button
android:id="@+id/nationButton"
android:onClick="nClickHandler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Press to Play 7 Nation Army"
android:layout_marginBottom="16dp"
/>
</android.support.design.widget.CoordinatorLayout>
答案 0 :(得分:1)
nClickHandler已经处理了按钮的单击事件,这意味着它已被按下。您在该方法中所需要的只是实例化toast并显示它:
public void nClickHandler(View v) {
switch (v.getId()) {
case R.id.nationButton:
Toast.makeText(this, "Just imagine listening to the song", Toast.LENGTH_LONG).show();
}
}
答案 1 :(得分:1)
您的代码不对。重新安排你的代码如下。
public class NationActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nation);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button nBut = (Button) findViewById(R.id.nationButton);
nBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nClickHandler();
}
});
}
public void nClickHandler() {
Toast nToast = Toast.makeText(this, "Just imagine listening to the song", Toast.LENGTH_LONG);
nToast.show();
}
}