是否有与ActionListener等效的Android?

时间:2017-02-10 02:42:46

标签: java android actionlistener

我基本上需要的是一个动作监听器,它记录是否已经点击了另一个可触摸的视图,以便可以将它们设置为启用(即,如果单击了imageview1,则现在可以单击imageview2而在它不能之前)。我环顾四周找到了ActionListener和ActionEvents,但在导入所有必需的软件包,更新我的SDK以及同步我的gradle文件后,Android Studio仍然无法识别这些选项。我似乎找不到让它发挥作用的方法。在让他们解决问题时我是否缺少某些东西,或者我可以使用另一种听众?

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="20dp"
        android:id="@+id/sq2"
        android:background="@color/preFocus"
        android:onClick="imagePress"
        android:layout_centerVertical="true"
        android:nextFocusDown="@+id/sq3"
        android:nextFocusUp="@+id/sq1"
        android:nextFocusLeft="@+id/sq4"
        android:nextFocusRight="@id/sq4"
         />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="250dp"
        android:background="@color/preFocus"
        android:id="@id/sq4"
        android:onClick="imagePress"
        android:enabled="false"
        android:layout_centerVertical="true"
        android:nextFocusDown="@id/sq3"
        android:nextFocusUp="@id/sq1"
        android:nextFocusLeft="@id/sq2"
        android:nextFocusRight="@id/sq2"
        />


    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="400dp"
        android:background="@color/preFocus"
        android:id="@id/sq3"
        android:onClick="imagePress"
        android:enabled="false"
        android:layout_centerHorizontal="true"
        android:nextFocusDown="@id/sq1"
        android:nextFocusUp="@id/sq1"
        android:nextFocusLeft="@id/sq2"
        android:nextFocusRight="@id/sq4"
        />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="50dp"
        android:background="@color/preFocus"
        android:id="@id/sq1"
        android:onClick="imagePress"
        android:enabled="false"
        android:layout_centerHorizontal="true"
        android:nextFocusDown="@id/sq3"
        android:nextFocusUp="@id/sq3"
        android:nextFocusLeft="@id/sq2"
        android:nextFocusRight="@id/sq4">
            <requestFocus />
    </ImageView>

</RelativeLayout>

爪哇:

package com.example.mcken.spinnertest;

import android.graphics.drawable.Drawable;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.support.v7.view.menu.MenuBuilder;
import android.support.v7.view.menu.MenuPopupHelper;
import android.util.Log;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;




import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.PopupMenu;

import java.lang.reflect.Field;

import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class MainActivity extends AppCompatActivity {

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

        RelativeLayout rl = (RelativeLayout) findViewById(R.id.activity_main);
        final int childcount = rl.getChildCount();

        for (int i = 0; i < childcount; i++){
            final View square = rl.getChildAt(i);
            final View nextSquare = rl.getChildAt(i+1);
            square.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent click){
                    nextSquare.setEnabled(true);
                }
            });
        }

    }

    public void clickableChange(){
        //what am I trying to do
        //set an actionlistener
        //if this is clicked
        //the "next" one is clickable

//        RelativeLayout rl = (RelativeLayout) findViewById(R.id.activity_main);
//        final int childcount = rl.getChildCount();
//
//        for (int i = 0; i < childcount; i++){
//            final View square = rl.getChildAt(i);
//            final View nextSquare = rl.getChildAt(i+1);
//            square.addActionListener(new ActionListener(){
//                public void actionPerformed(ActionEvent click){
//                    nextSquare.setEnabled(true);
//                }
//            });
//        }
    }

    public void imagePress(View test){
        int id = test.getId();
        ImageView picture = (ImageView) findViewById(id);

        final int pictureId = id;
        picture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                PopupMenu popup = new PopupMenu(MainActivity.this, view);
                popup.getMenuInflater().inflate(R.menu.popup_menu,popup.getMenu());


                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
                    public boolean onMenuItemClick(MenuItem selection){
                        ImageView thisThing = (ImageView) findViewById(pictureId);
                        Drawable icon = selection.getIcon();

                        thisThing.setImageDrawable(icon);
                        return true;
                    }
                });

                popup.show();


            }
        });
    }
}

0 个答案:

没有答案