使用onClick侦听器事件显示图像

时间:2017-07-24 02:42:14

标签: java android xml onclicklistener

我的android开发经验非常少(谢天谢地,有一些堆栈溢出。一直在帮助我)。我想要做的是在单击特定菜单选项时显示图像视图。我已经让菜单/子菜单正常工作,我知道我可以使用android:visibility="invisible"使图像不可见,我想我可以使用imageView.visiblity="visible"在java代码中看到它我认为(如果不是请更正)我)。我被告知我可以使用“onClickListener事件”来执行此操作,但我读过的文档对我没有任何帮助。  如果有人能帮助我,我将不胜感激。

请参阅以下照片和我的Java / XML代码。

how it's supposed to start after clicking + button on the bottom after clicking "Plain Image" 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>

3 个答案:

答案 0 :(得分:1)

就这样。

Imageview.setVisibility(View.GONE);

Imageview.setVisibility(View.VISIBLE);

答案 1 :(得分: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);
 }
}

希望这有帮助。

答案 2 :(得分:0)

您可能希望首先获得对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
}

然后在onContextItemSelected(由@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