2017-07-24 42 views
0

我已經很少有Android開發經驗(幸好有stackoverflow,一直在幫我很多)。我想要做的是在點擊某個特定菜單選項時顯示一個imageview。我有菜單/子菜單正常工作,我知道我可以使圖像看不見android:visibility="invisible",我想我可以使它在Java代碼中可見imageView.visiblity="visible"我認爲(如果不是,請更正我)。我被告知我可以使用「onClickListener事件」來做到這一點,但我讀過的文檔對我沒有任何幫助。 如果任何人都可以請幫助我,這將不勝感激。用onClick監聽器事件顯示圖像

請參考以下照片和我的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) 我的Java代碼到目前爲止(也是我在Java中創建我的菜單/子菜單,不是我不知道XML,如果這是不好的做法,但希望有人會告訴我的。):到目前爲止

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"); 
    } 
} 

我的XML代碼:

<?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

是否要在菜單項上顯示/隱藏ImageView點擊 –

+0

是的。基本上當有人點擊以下任一選項時:'''sub.add(Menu.NONE,MENU_SORT_BY_NAME,Menu.NONE,「GridVu」); sub.add(Menu.NONE,MENU_SORT_BY_ADDRESS,Menu.NONE,「Story」);'''imageview顯示@SachinBahukhandi –

回答

1

只是做等。

Imageview.setVisibility(View.GONE); 

而且

Imageview.setVisibility(View.VISIBLE); 
+0

這是否工作過?@Marc Karam –

+0

我不知道我會把你的代碼放在哪裏。因爲當用戶點擊這些菜單選項中的一個時,它應該變得可見,這些菜單選項顯示爲 '''sub.add(Menu.NONE,MENU_SORT_BY_NAME,Menu.NONE,「GridVu」); sub.add(Menu.NONE,MENU_SORT_BY_ADDRESS,Menu.NONE,「Story」);''' –

+0

只需將我的代碼添加到存在菜單的視圖的onclick中...例如。如果菜單是一個列表視圖,然後將代碼添加到列表視圖的onclick –

0

我覺得OnClickListener是行不通的,而不是OnContextItemSelected將幫助你:

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

希望這有助於。

0

你可能想先有你的ImageView的引用,

​​

然後在onContextItemSelected(如decripbed由@SachinBahukhandi)就可以了,

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

按照Android文檔,Use onContextItemSelected(android.view.MenuItem) to know when an item has been selected