2017-07-24 2 views
0

J'ai très peu d'expérience de développement Android (heureusement, il y a eu stackoverflow m'a beaucoup aidé). Ce que j'essaie de faire, c'est d'afficher une image quand on clique sur une option de menu spécifique. J'ai le menu/sous-menu fonctionne correctement et je sais que je peux rendre l'image invisible avec android:visibility="invisible" et je pense que je peux le rendre visible en code Java avec imageView.visiblity="visible" Je pense (si non, corrigez-moi s'il vous plaît). On m'a dit que je pourrais utiliser un événement "onClickListener" pour le faire, mais la documentation que j'ai lue n'a été d'aucune aide pour moi. Si quelqu'un peut m'aider s'il vous plaît, ce serait grandement apprécié.Affichage d'une image avec l'événement onClick Listener

Veuillez vous référer aux photos suivantes et à mon code Java/XML.

how it's supposed to start after clicking + button on the bottom after clicking either option (also the background should be dimmed, no idea how to do that) code Java Ma jusqu'à présent (. J'ai aussi créé mon menu/sous-menu en Java, pas XML, je ne sais pas si cela est une mauvaise pratique, mais nous espérons que quelqu'un va me dire):

package com.example.android.postvu; 

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.ContextMenu; 
import android.view.SubMenu; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MainActivity extends AppCompatActivity { 

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

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.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); 
} 


public void myOnClickMethod(View v) { 
    registerForContextMenu(v); 
    openContextMenu(v); 
} 

final int CONTEXT_MENU_VIEW = 1; 
final int CONTEXT_MENU_EDIT = 2; 
final int MENU_SORT = 3; 
final int MENU_SORT_BY_NAME = 4; 
final int MENU_SORT_BY_ADDRESS = 5; 


@Override 
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 
    menu.setHeaderTitle("Options"); 
    menu.add(Menu.NONE, CONTEXT_MENU_VIEW, Menu.NONE, "Take Photo"); 
    menu.add(Menu.NONE, CONTEXT_MENU_EDIT, Menu.NONE, "Photo Album"); 
    SubMenu sub=menu.addSubMenu(Menu.NONE, MENU_SORT, Menu.NONE, "Plain Image"); 
    sub.add(Menu.NONE, MENU_SORT_BY_NAME, Menu.NONE, "GridVu"); 
    sub.add(Menu.NONE, MENU_SORT_BY_ADDRESS, Menu.NONE, "Story"); 
    } 
} 

Mon code XML jusqu'à présent:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:showIn="@layout/activity_main" 
tools:context="com.example.android.postvu.MainActivity"> 

<ImageView 
    android:id="@+id/imageView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:contentDescription="@string/background" 
    android:scaleType="centerCrop" 
    android:src="@drawable/grid" 
    tools:layout_editor_absoluteY="0dp" 
    android:layout_marginRight="7dp" 
    android:layout_marginEnd="7dp" 
    tools:ignore="MissingConstraints" /> 

<TextView 
    android:id="@+id/textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="8dp" 
    android:layout_marginRight="8dp" 
    android:layout_marginTop="8dp" 
    android:text="@string/image_text_editor" 
    android:textColor="@android:color/black" 
    android:textSize="28sp" 
    android:textStyle="bold" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    tools:text="Image Text Editor" 
    android:layout_marginStart="8dp" 
    android:layout_marginEnd="8dp" /> 


<Button 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:layout_marginBottom="8dp" 
    android:layout_marginLeft="8dp" 
    android:layout_marginRight="8dp" 
    android:layout_marginTop="8dp" 
    android:background="@mipmap/ic_launcher" 
    android:scaleType="centerCrop" 
    android:onClick="myOnClickMethod" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toBottomOf="@+id/textView" 
    app:layout_constraintVertical_bias="0.98" 
    android:id="@+id/button" 
    android:layout_marginStart="8dp" 
    android:layout_marginEnd="8dp" /> 

<ImageView 
    android:id="@+id/imageView3" 
    android:layout_width="450dp" 
    android:layout_height="450dp" 
    android:layout_marginBottom="8dp" 
    android:layout_marginLeft="8dp" 
    android:layout_marginRight="8dp" 
    android:layout_marginTop="8dp" 
    android:contentDescription="@string/white_background" 
    android:visibility="invisible" 
    app:layout_constraintBottom_toBottomOf="@+id/button" 
    app:layout_constraintLeft_toLeftOf="@+id/imageView2" 
    app:layout_constraintRight_toRightOf="@+id/imageView2" 
    app:layout_constraintTop_toTopOf="@+id/textView" 
    app:srcCompat="@mipmap/white" /> 

</android.support.constraint.ConstraintLayout> 
+0

Voulez-vous afficher/masquer l'élément ImageView sur le menu, cliquez sur –

+0

ouais. En fait, lorsque quelqu'un clique sur l'une des options suivantes: '' 'sub.add (Menu.NONE, MENU_SORT_BY_NAME, Menu.NONE," GridVu "); sub.add (Menu.NONE, MENU_SORT_BY_ADDRESS, Menu.NONE, "Story"); '' 'l'image s'affiche @SachinBahukhandi –

Répondre

1

Il suffit de faire comme.

Imageview.setVisibility(View.GONE); 

Et

Imageview.setVisibility(View.VISIBLE); 
+0

Cela fonctionne ou pas @Marc Karam –

+0

Je ne sais pas où je mettrais votre code. car il est censé devenir visible lorsque l'utilisateur clique sur l'une de ces options de menu qui apparaissent '' 'sub.add (Menu.NONE, MENU_SORT_BY_NAME, Menu.NONE," GridVu "); sub.add (Menu.NONE, MENU_SORT_BY_ADDRESS, Menu.NONE, "Story"); '' ' –

+0

Il suffit d'ajouter mon code à onclick of view où le menu est présent ... par exemple. Si le menu est un listview, ajoutez le code à onclick de listview –

0

Je pense que OnClickListener ne fonctionnerait pas à la place OnContextItemSelected pourrait vous aider:

@Override 
public boolean onContextItemSelected(MenuItem item) { 

switch (item.getItemId()) { 
case CONTEXT_MENU_VIEW: 
    any_function();//add your functionality here i.e. what you want to do 
    return true; 
case CONTEXT_MENU_EDIT: 
    **confirmDelete**(); 
    return true; 
default: 
    return super.onContextItemSelected(item); 
} 
} 

Hope this helps.

0

Vous voudrez peut-être d'abord d'avoir une référence à votre ImageView,

public class MainActivity extends AppCompatActivity { 

    private ImageView mImageView; 

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

     // other codes 

     // R.id.imageView3 where imageView3 is the ID in 
     // <ImageView android:id="@+id/imageView3" 
     mImageView = (ImageView) findViewById(R.id.imageView3); 

     // other codes 
    } 

    // other codes 
} 

Puis onContextItemSelected (comme decripbed par @SachinBahukhandi), vous pouvez,

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    switch (item.getItemId()){ 
     case CONTEXT_MENU_VIEW: 
      mImageView.setVisibility(View.VISIBLE); 
      //mImageView.setVisibility(View.INVISIBLE); 
      break; 

     case CONTEXT_MENU_EDIT: 
      //mImageView.setVisibility(View.VISIBLE); 
      //mImageView.setVisibility(View.INVISIBLE); 
      break; 

     case MENU_SORT: 
      //mImageView.setVisibility(View.VISIBLE); 
      //mImageView.setVisibility(View.INVISIBLE); 
      break; 

     case MENU_SORT_BY_NAME: 
      //mImageView.setVisibility(View.VISIBLE); 
      //mImageView.setVisibility(View.INVISIBLE); 
      break; 

     case MENU_SORT_BY_ADDRESS: 
      //mImageView.setVisibility(View.VISIBLE); 
      //mImageView.setVisibility(View.INVISIBLE); 
      break; 
    } 

    return super.onContextItemSelected(item); 
} 

Selon Documentation Android, Use onContextItemSelected(android.view.MenuItem) to know when an item has been selected.