2016-07-20 8 views
0

ボタンを押すたびに私のトーストメッセージがポップアップしない理由を理解しようとしています。ボタンを押すたびに何も起こりません。私はいくつかの研究をしており、これを理解することはできません。私はこれに非常に新しいです。私はアンドロイドの開発者になる方法を学ぼうとする学生です。誰も助けることができますか?私のトーストはなぜ機能しないのですか? (アンドロイド)

これは私の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> 
+1

ボタンを1秒以上押し続け、ポップアップが表示されているかどうかを確認し、コメントを書きます(タグを付けてください)。私は 'if(nBut.isPressed()){'がコード実行がこの行に達したときにfalseになるので、トーストをしません。 – Vucko

+3

'if(nBut.isPressed())'条件を使用する理由? –

+0

ボタンを持っているときにトーストが意図的に表示されるようにしたい場合にのみ、同じことが考えられました。D – Vucko

答えて

1

nClickHandlerはすでにそれがすでに押されて意味ボタンのクリックイベントを処理しています。その方法で必要なのは、トーストをインスタンス化して表示することだけです。

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(); 
    } 
} 
+0

私はこれを試して、それが動作していない。 –

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(); 
    } 
} 
+0

これは私のために働いていません。 –

+0

@DannyHiggins私は答えを –

+0

まだ更新していない参照してください。 –

関連する問題