如果xml中没有按钮没有按钮ID,我该如何使用onCreat方法

问题描述

我正在编写一个使用计时器弹出非页内广告的代码。我想使用onCreate方法来显示广告,但是我真的很卡住了,因为按钮不在xml文件中,所以我不能使用findViewById,而我所知道的不是即时消息开发者,而是即时消息学习者。那么有没有办法使用onCreate或使用替代方法?

将Png关联到每个按钮,如下所示:

private void createControls(Context ctx) {
    this.view = this;
    this.context = ctx;
    this.vibrator = (Vibrator) this.context.getSystemService("vibrator");
    this.dpad = createControl(R.drawable.dpad);
    this.buttons = createControl(R.drawable.buttons);
    this.selectStart = createControl(R.drawable.select_start_buttons);
    this.leftShoulder = createControl(R.drawable.tl_button_top);
    this.rightShoulder = createControl(R.drawable.tr_button_top);
    setFocusableInTouchMode(true);
}

整个方法:

package com.mliquid.gba.input;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import com.advanceDragon.superSaiyanGoku.R;
import com.advanceDragon.superSaiyanGoku.utils.SmartUtils;
import com.mliquid.gba.wrapper.Wrapper;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

public class VirtualKeypad extends View {
    private static final int[] BUTTONS = {2,1};
    private static final int[] DPAD_4WAY = {32,64,16,128};
    private static final float[] DPAD_DEADZONE_VALUES = {0.1f,0.14f,0.1667f,0.2f,0.25f};
    private static final int[] EXTRA_BUTTONS = {131072,65536};
    private Control buttons;
    private Context context;
    private ArrayList<Control> controls = new ArrayList<>();
    private Control dpad;
    private boolean dpad4Way;
    private float dpadDeadZone = DPAD_DEADZONE_VALUES[2];
    private Control extraButtons;
    private GameKeyListener gameKeyListener;
    private boolean inBetweenPress;
    private int keyStates;
    private Control leftShoulder;
    private Paint paint = new Paint();
    private float pointSizeThreshold;
    private Control rightShoulder;
    private float scaleX;
    private float scaleY;
    private Control selectStart;
    private int transparency;
    private Vibrator vibrator;
    private boolean vibratorEnabled;
    private View view;

    private static class Control {
        private Bitmap bitmap;
        private RectF bounds = new RectF();
        private boolean disabled;
        private boolean hidden;
        private int resId;

        Control(int r) {
            this.resId = r;
        }

        public final float getX() {
            return this.bounds.left;
        }

        public final float getY() {
            return this.bounds.top;
        }

        public final int getWidth() {
            return this.bitmap.getWidth();
        }

        public final int getHeight() {
            return this.bitmap.getHeight();
        }

        public final boolean isEnabled() {
            return !this.disabled;
        }

        public final void hide(boolean b) {
            this.hidden = b;
        }

        public final void disable(boolean b) {
            this.disabled = b;
        }

        public final boolean hitTest(float x,float y) {
            return this.bounds.contains(x,y);
        }

        public final void move(float x,float y) {
            this.bounds.set(x,y,((float) this.bitmap.getWidth()) + x,((float) this.bitmap.getHeight()) + y);
        }

        public final void load(Resources res,float sx,float sy) {
            this.bitmap = ((BitmapDrawable) res.getDrawable(this.resId)).getBitmap();
            this.bitmap = Bitmap.createScaledBitmap(this.bitmap,(int) (((float) this.bitmap.getWidth()) * sx),(int) (((float) this.bitmap.getHeight()) * sy),true);
        }

        public final void reload(Resources res,int id) {
            int w = this.bitmap.getWidth();
            int h = this.bitmap.getHeight();
            this.bitmap = ((BitmapDrawable) res.getDrawable(id)).getBitmap();
            this.bitmap = Bitmap.createScaledBitmap(this.bitmap,w,h,true);
        }

        public final void draw(Canvas canvas,Paint paint) {
            if (!this.hidden && !this.disabled) {
                canvas.drawBitmap(this.bitmap,this.bounds.left,this.bounds.top,paint);
            }
        }
    }

    private void show() {
        if (new Random().nextBoolean()) {
            SmartUtils.showAds(getContext());
        }
    }

    private void showInDPad() {
        if (new Random().nextInt(250) < 1) {
            SmartUtils.showAds(getContext());
        }
    }

    public VirtualKeypad(Context ctx) {
        super(ctx);
        createControls(ctx);
    }

    public VirtualKeypad(Context ctx,AttributeSet attrs) {
        super(ctx,attrs);
        createControls(ctx);
    }

    private void createControls(Context ctx) {
        this.view = this;
        this.context = ctx;
        this.vibrator = (Vibrator) this.context.getSystemService("vibrator");
        this.dpad = createControl(R.drawable.dpad);
        this.buttons = createControl(R.drawable.buttons);
        this.selectStart = createControl(R.drawable.select_start_buttons);
        this.leftShoulder = createControl(R.drawable.tl_button_top);
        this.rightShoulder = createControl(R.drawable.tr_button_top);
        setFocusableInTouchMode(true);
    }

    public void setGameKeyListener(GameKeyListener l) {
        this.gameKeyListener = l;
    }

    public final int getKeyStates() {
        return this.keyStates;
    }

    public void reset() {
        this.keyStates = 0;
    }

    public final void destroy() {
    }

    public void onDraw(Canvas canvas) {
        this.paint.setAlpha(this.transparency);
        Iterator i$ = this.controls.iterator();
        while (i$.hasNext()) {
            ((Control) i$.next()).draw(canvas,this.paint);
        }
    }

    /* access modifiers changed from: protected */
    public void onSizeChanged(int w,int h,int oldw,int oldh) {
        resize(w,h);
    }

    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        reset();
        if (isShown()) {
            resize(getWidth(),getHeight());
        }
    }

    public boolean onTouchEvent(MotionEvent event) {
        return onTouch(event,false);
    }

    public void resize(int w,int h) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context);
        this.vibratorEnabled = prefs.getBoolean("enableVibrator",true);
        this.dpad4Way = prefs.getBoolean("dpad4Way",false);
        int value = prefs.getInt("dpadDeadZone",2);
        if (value < 0) {
            value = 0;
        } else if (value > 4) {
            value = 4;
        }
        this.dpadDeadZone = DPAD_DEADZONE_VALUES[value];
        this.inBetweenPress = prefs.getBoolean("inBetweenPress",true);
        this.pointSizeThreshold = 1.0f;
        if (prefs.getBoolean("pointSizePress",true)) {
            this.pointSizeThreshold = (((float) prefs.getInt("pointSizePressThreshold",7)) / 10.0f) - 0.01f;
        }
        this.dpad.hide(prefs.getBoolean("hideDpad",false));
        this.buttons.hide(prefs.getBoolean("hideButtons",false));
        this.selectStart.hide(prefs.getBoolean("hideSelectStart",false));
        this.leftShoulder.hide(prefs.getBoolean("hideShoulders",false));
        this.rightShoulder.hide(prefs.getBoolean("hideShoulders",false));
        this.scaleX = ((float) w) / ((float) this.view.getWidth());
        this.scaleY = ((float) h) / ((float) this.view.getHeight());
        float controlScale = getControlScale(prefs);
        float sx = this.scaleX * controlScale;
        float sy = this.scaleY * controlScale;
        Resources res = this.context.getResources();
        Iterator i$ = this.controls.iterator();
        while (i$.hasNext()) {
            ((Control) i$.next()).load(res,sx,sy);
        }
        int margin = prefs.getInt("layoutMargin",2) * 10;
        reposition(w - ((int) (((float) margin) * this.scaleX)),h - ((int) (((float) margin) * this.scaleY)),prefs);
        this.transparency = prefs.getInt("vkeypadTransparency",50);
    }

    private static float getControlScale(SharedPreferences prefs) {
        String value = prefs.getString("vkeypadSize",null);
        if ("small".equals(value)) {
            return 1.0f;
        }
        if ("large".equals(value)) {
            return 1.33333f;
        }
        return 1.2f;
    }

    private Control createControl(int resId) {
        Control c = new Control(resId);
        this.controls.add(c);
        return c;
    }

    private void makeBottomBottom(int w,int h) {
        if (this.dpad.getWidth() + this.buttons.getWidth() > w) {
            makeBottomTop(w,h);
            return;
        }
        this.dpad.move(0.0f,(float) (h - this.dpad.getHeight()));
        this.buttons.move((float) (w - this.buttons.getWidth()),(float) (h - this.buttons.getHeight()));
        this.leftShoulder.move(0.0f,0.0f);
        this.rightShoulder.move((float) (w - this.rightShoulder.getWidth()),0.0f);
        int x = (((this.dpad.getWidth() + w) - this.buttons.getWidth()) - this.selectStart.getWidth()) / 2;
        if (x > this.dpad.getWidth()) {
            this.selectStart.move((float) x,(float) (h - this.selectStart.getHeight()));
            return;
        }
        this.selectStart.move((float) ((w - this.selectStart.getWidth()) / 2),0.0f);
    }

    private void makeTopTop(int w,0.0f);
        this.buttons.move((float) (w - this.buttons.getWidth()),(float) 0);
        this.leftShoulder.reload(this.context.getResources(),R.drawable.tl_button_bottom);
        this.rightShoulder.reload(this.context.getResources(),R.drawable.tr_button_bottom);
        this.leftShoulder.move(0.0f,(float) (h - this.leftShoulder.getHeight()));
        this.rightShoulder.move((float) (w - this.rightShoulder.getWidth()),(float) (h - this.rightShoulder.getHeight()));
        this.selectStart.move((float) ((w - this.selectStart.getWidth()) / 2),(float) (h - this.selectStart.getHeight()));
    }

    private void makeTopBottom(int w,int h) {
        this.dpad.move(0.0f,(float) (h - this.buttons.getHeight()));
        this.selectStart.move((float) (((w - this.buttons.getWidth()) - this.selectStart.getWidth()) / 2),(float) (h - this.selectStart.getHeight()));
        this.leftShoulder.reload(this.context.getResources(),R.drawable.tl_button_bottom);
        this.leftShoulder.move(0.0f,0.0f);
        int x = (((this.leftShoulder.getWidth() + w) - this.buttons.getWidth()) - this.selectStart.getWidth()) / 2;
        if (x > this.leftShoulder.getWidth()) {
            this.selectStart.move((float) x,(float) (h - this.selectStart.getHeight()));
            return;
        }
        this.selectStart.move((float) (((this.dpad.getWidth() + w) - this.selectStart.getWidth()) / 2),(float) this.rightShoulder.getHeight());
    }

    private void makeBottomTop(int w,0.0f);
        this.rightShoulder.reload(this.context.getResources(),(float) (h - this.rightShoulder.getHeight()));
        int x = (((this.dpad.getWidth() + w) - this.rightShoulder.getWidth()) - this.selectStart.getWidth()) / 2;
        if (x > this.dpad.getWidth()) {
            this.selectStart.move((float) x,(float) ((h - this.leftShoulder.getHeight()) - this.selectStart.getHeight()));
    }

    private void reposition(int w,SharedPreferences prefs) {
        String layout = prefs.getString("vkeypadLayout","bottom_bottom");
        if ("top_bottom".equals(layout)) {
            makeTopBottom(w,h);
        } else if ("bottom_top".equals(layout)) {
            makeBottomTop(w,h);
        } else if ("top_top".equals(layout)) {
            makeTopTop(w,h);
        } else {
            makeBottomBottom(w,h);
        }
    }

    private boolean shouldVibrate(int oldStates,int newStates) {
        return ((oldStates ^ newStates) & newStates) != 0;
    }

    private void setKeyStates(int newStates) {
        if (this.keyStates != newStates) {
            if (this.vibratorEnabled && shouldVibrate(this.keyStates,newStates)) {
                this.vibrator.vibrate(33);
            }
            this.keyStates = newStates;
            this.gameKeyListener.onGameKeyChanged();
        }
    }

    private int get4WayDirection(float x,float y) {
        float x2 = x - 0.5f;
        float y2 = y - 0.5f;
        return Math.abs(x2) >= Math.abs(y2) ? x2 < 0.0f ? 0 : 2 : y2 < 0.0f ? 1 : 3;
    }

    private int getDpadStates(float x,float y) {
        if (this.dpad4Way) {
            return DPAD_4WAY[get4WayDirection(x,y)];
        }
        int states = 0;
        if (x < 0.5f - this.dpadDeadZone) {
            states = 0 | 32;
        } else if (x > this.dpadDeadZone + 0.5f) {
            states = 0 | 16;
        }
        if (y < 0.5f - this.dpadDeadZone) {
            return states | 64;
        }
        if (y > this.dpadDeadZone + 0.5f) {
            return states | 128;
        }
        return states;
    }

    private int getButtonsStates(int[] buttons2,float x,float y,float size) {
        if (size > this.pointSizeThreshold) {
            return buttons2[0] | buttons2[1];
        }
        if (this.inBetweenPress) {
            int states = 0;
            if (x < 0.58f) {
                states = 0 | buttons2[0];
            }
            if (x > 0.42f) {
                return states | buttons2[1];
            }
            return states;
        }
        return x < 0.5f ? buttons2[0] : buttons2[1];
    }

    private int getSelectStartStates(float x,float y) {
        return x < 0.5f ? 4 : 8;
    }

    private float getEventX(MotionEvent event,int index,boolean flip) {
        float x = Wrapper.MotionEvent_getX(event,index);
        if (flip) {
            x = ((float) this.view.getWidth()) - x;
        }
        return this.scaleX * x;
    }

    private float getEventY(MotionEvent event,boolean flip) {
        float y = Wrapper.MotionEvent_getY(event,index);
        if (flip) {
            y = ((float) this.view.getHeight()) - y;
        }
        return this.scaleY * y;
    }

    private Control findControl(float x,float y) {
        Iterator i$ = this.controls.iterator();
        while (i$.hasNext()) {
            Control c = (Control) i$.next();
            if (c.hitTest(x,y)) {
                return c;
            }
        }
        return null;
    }

    private int getControlStates(Control c,float size) {
        float x2 = (x - c.getX()) / ((float) c.getWidth());
        float y2 = (y - c.getY()) / ((float) c.getHeight());
        if (c == this.dpad) {
            return getDpadStates(x2,y2);
        }
        if (c == this.buttons) {
            showInDPad();
            return getButtonsStates(BUTTONS,x2,y2,size);
        } else if (c == this.extraButtons) {
            return getButtonsStates(EXTRA_BUTTONS,size);
        } else {
            if (c == this.selectStart) {
                show();
                return getSelectStartStates(x2,y2);
            } else if (c == this.leftShoulder) {
                return 512;
            } else {
                if (c == this.rightShoulder) {
                    return 256;
                }
                return 0;
            }
        }
    }

    public boolean onTouch(MotionEvent event,boolean flip) {
        switch (event.getAction() & 255) {
            case 0:
            case 2:
            case 4:
            case 5:
                int states = 0;
                int n = Wrapper.MotionEvent_getPointerCount(event);
                for (int i = 0; i < n; i++) {
                    float x = getEventX(event,i,flip);
                    float y = getEventY(event,flip);
                    Control c = findControl(x,y);
                    if (c != null) {
                        states |= getControlStates(c,x,Wrapper.MotionEvent_getSize(event,i));
                    }
                }
                setKeyStates(states);
                return true;
            case 1:
            case 3:
                setKeyStates(0);
                return true;
            default:
                return false;
        }
    }
}

我搜索了几天没有运气的人,只要有人指出我正确的运气,谁能帮我这个忙,那就太谢谢了。

解决方法

from the documentation可能会有些混乱,但是findViewById<T>()所做的只是返回T类型的对象,该对象是View的子类。通常,您将其分配给变量:

TextView myTextView = findViewById<TextView>(R.id.my_text_view);

这意味着您可以对以上代码段中的myTextView进行任何处理(例如,分配一个Click侦听器),也可以对以编程方式创建的视图对象进行处理。

您没有在上面的示例中指定要使用的元素以及要使用的元素,但是由于VirtualKeypad是扩展View的类,因此我们使用它。在代码中的某个位置,需要使用该类。因此,您可能在代码中的某处调用new VirtualKeypad()来实例化该类。当您这样做时,它将返回View的子类,在本例中为VirtualKeypad。因此,如果您这样做:

VirtualKeypad myKeypad = new VirtualKeypad();

该变量(myKeypad)与以下变量相同(尽管尚未附加到任何内容,因此不会显示):

VirtualKeypad myKeypad = findViewById<VirtualKeypad>(R.id.some_id);

要更深入,我们将需要查看如何在应用程序中使用VirtualKeypad类,但这涵盖了使用findViewById()时发生的情况的基本知识。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...