2016-05-29 7 views
0

私はテストや学習の目的で小さなものを作成しました。画像ボタンで構成され、画像ボタンを押しながらトーストを表示します。イメージボタンクリックイベントが機能していません

私はトーストメッセージがすべて表示されないように変更しました。コードには単一のエラーまたは警告はありません。

最後の日に、私はここにコードを掲載し、回答を得られませんでした。

今回はソースコードをアップロードしました。誰かがこの簡単な解決策を解決できることを願っています。

https://drive.google.com/file/d/0B6Ax58vO7SvZcGRwNFlyYzhqZFk/view?usp=sharing

+0

あなたは "ContentMain" であなたの "のonClick" 機能でブレークポイントを置くことができますそのクラスの後にその線量を確認するクリックかどうか?それが "v.getId()"の値が何であるかを見るために火災のチェックがあるかどうかを調べます。 – pooyan

+0

コードはどこに投稿しましたか?もう一度それを共有できますか? – Elye

+0

私はあなたがここに投稿したことを発見しましたhttp://stackoverflow.com/questions/37485477/image-button-event-not-working、いくつかの回答があります。 – Elye

答えて

0

だけでは、レイアウトファイル(XML)で任意のonClickイベントを設定した場合、あなたが持っている

をRememeber

public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.imageButton: 
      Toast.makeText(getApplicationContext(),"You download is resumed",Toast.LENGTH_LONG).show(); 
      break; 
    } 
} 

をコンテンツからこのコードを削除し、MainActivity.java

に置きますそのレイアウトを使用する親アクティビティにメソッドを作成します。

これは完全なコードです。

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="switchmode.lteonly.lteonlyswitch.MainActivity" 
tools:showIn="@layout/app_bar_main"> 

<ImageButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageButton" 

    android:clickable="true" 
    android:layout_centerVertical="true" 
    android:layout_centerInParent="true" 
    android:src="@drawable/myimage" 
    android:onClick="onClick"/> 

MainActivity.java

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
} 

@Override 
public void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     super.onBackPressed(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.nav_camera) { 
     // Handle the camera action 
    } else if (id == R.id.nav_gallery) { 

    } else if (id == R.id.nav_slideshow) { 

    } else if (id == R.id.nav_manage) { 

    } else if (id == R.id.nav_share) { 

    } else if (id == R.id.nav_send) { 

    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.imageButton: 
      Toast.makeText(getApplicationContext(),"You download is resumed",Toast.LENGTH_LONG).show(); 
      break; 
    } 
} 

}

Contentmain.java

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.content_main); 
    ImageButton imgButton =(ImageButton)findViewById(R.id.imageButton); 
    // imgButton.setOnClickListener(this); 
} 
+1

あなたは素晴らしいです。あなたは理由を説明することで答えを解決しました。高等学校高等学校より。 –

+0

あなたは大歓迎です:) –

関連する問題