dPad是什么意思?

dPad是什么意思?,第1张

dPad常见于各大安卓模拟器,相当于手机中的“上下左右中”五个按键(也可用于游戏手柄的按键)。

安卓模拟器是一款模拟软件。它能在电脑上模拟安卓手机系统,并能安装、使用、卸载安卓应用软件,让你在电脑上也能体验操作安卓系统的全过程。dPad是安卓模拟器可以模拟的一个功能

编写代码:

[xhtml] view plaincopy

<?xml version="1.0" encoding="utf-8"?>

<AbsoluteLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#FFFFFF"

>

<Button

android:id="@+id/myButton1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="move me"

android:layout_x="20px"

android:layout_y="40px" />

</AbsoluteLayout>

activity代码:

[java] view plaincopy

package cn.com.chenzheng_java

import android.app.Activity

import android.os.Bundle

import android.util.DisplayMetrics

import android.view.KeyEvent

import android.widget.AbsoluteLayout

import android.widget.Button

import android.widget.Toast

/**

* @description 控制手机的上下左右四个方向键

* @author chenzheng_java

*

*/

public class DpadActivity extends Activity {

Button button

DisplayMetrics metrics = new DisplayMetrics()

int screenx = 0 //屏幕宽度

int screeny = 0 //屏幕高度

int buttonWidth = 80//按钮宽度

int buttonHeight = 40 // 按钮高度

int currentX = 0// 按钮的当前x坐标

int currentY = 0// 按钮的当前Y坐标

int step = 0//移动时候的步长

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState)

setContentView(R.layout.dpad)

button = (Button) findViewById(R.id.myButton1)

getWindowManager().getDefaultDisplay().getMetrics(metrics)

screenx = metrics.widthPixels

screeny = metrics.heightPixels

/* buttonWidth = button.getWidth()

buttonHeight = button.getHeight()*/

currentX = (screenx-buttonWidth)/2

currentY = (screeny-buttonHeight)/2

step = 2

button.setLayoutParams(new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, currentX, currentY))

}

/**

* 当前后左右键被按下的时候,被触发(这里可是有前提的哦,那就是当前的activity中必须没有view正在监听按键

* ,例如:当前如果有一个EditText正在等待输入,当我们按下dpad时,不会触发事件哦)

* Activity.onKeyDown()

当某个键被按下时会触发,但不会被任何的该Activity内的任何view处理。

默认按下KEYCODE_BACK键后会回到上一个Activity。

*/

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

switch (keyCode) {

case KeyEvent.KEYCODE_DPAD_DOWN://按向下键

moveDown()

break

case KeyEvent.KEYCODE_DPAD_UP:// 按向上键

moveUp()

case KeyEvent.KEYCODE_DPAD_LEFT://按向左键

moveLeft()

case KeyEvent.KEYCODE_DPAD_RIGHT://按向右键

moveRight()

default:

break

}

return super.onKeyDown(keyCode, event)

}

@SuppressWarnings("deprecation")

private void moveDown(){

int temp = currentY+step

if(temp>(screeny-buttonHeight)){

showToast("到头了哦!")

button.setLayoutParams(new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, screenx, screeny-buttonHeight))

}

else{

currentY = currentY+step

AbsoluteLayout.LayoutParams params =

new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, currentX, currentY)

button.setLayoutParams(params)

}

//button.setLayoutParams(new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, currentX, currentY-2))

}

@SuppressWarnings("deprecation")

private void moveUp(){

int temp = currentY-step

if(temp<=0){

showToast("往上到头了哦!")

button.setLayoutParams(new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, screenx, 0))

}

else{

currentY = currentY-step

AbsoluteLayout.LayoutParams params =

new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, currentX, currentY)

button.setLayoutParams(params)

}

}

@SuppressWarnings("deprecation")

private void moveLeft(){

int temp = currentX-step

if(temp<=0){

showToast("往左边到头了哦!")

button.setLayoutParams(new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, 0, screeny))

}

else{

currentX = currentX-step

AbsoluteLayout.LayoutParams params =

new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, currentX, currentY)

button.setLayoutParams(params)

}

}

@SuppressWarnings("deprecation")

private void moveRight(){

int temp = currentX+step

if(temp>=(screenx-buttonWidth)){

showToast("往右边到头了哦!")

button.setLayoutParams(new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, screenx-buttonWidth, currentY))

}

else{

currentX = currentX+step

AbsoluteLayout.LayoutParams params =

new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, currentX, currentY)

button.setLayoutParams(params)

}

}

/**

* 弹出提示信息

* @param text 提示信息

*/

private void showToast(String text){

Toast.makeText(this, text, Toast.LENGTH_LONG).show()

圣象燃气热水器非常好。优点是热效率高、加热速度快、温度调节稳定、可多人连续使用,冬天在厨房里就可以随时来热水,方便。水温恒定,购置费用便宜。一般家庭,使用8升的机器就够了。选用优质的不锈钢及高分子材料制成,具有很好的耐高温,抗氧化,耐用的特点,加热时间一般5分钟,具有良好的防干烧保护、防烫伤保护特点,以及断电记忆功能,安全性高。


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/133163.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-16
下一篇2023-03-16

发表评论

登录后才能评论

评论列表(0条)

    保存