如何在重力加速度监听器中实现button被按下的逻辑?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在重力加速度监听器中实现button被按下的逻辑?相关的知识,希望对你有一定的参考价值。

<p>
    <br/>
</p>我这有一个小问题,能否帮忙给个思路。
 我用
button_w.setPressed(false);
想在加速度监听器中设置按钮被按下,并执行按钮被按下相关的逻辑,可是上面那个不管用,虽然按下的图片改变了,但是并没有启动onClcik();中的逻辑,有什么好办法实现吗?


Java code

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
 
package com.example.carcontroler;
 
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends Activity implements OnClickListener {
    private SensorManager sensorManager;
    private Sensor sensor = null;
    private boolean sensorFlag = false;
    private mySensorEventListener listener = new mySensorEventListener();
 
    private TextView xyz = null;
    private ImageButton button_w = null;
    private ImageButton button_s = null;
    private ImageButton button_a = null;
    private ImageButton button_d = null;
    private ImageButton button_g = null;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        xyz = (TextView) findViewById(R.id.textview1);
        button_w = (ImageButton) findViewById(R.id.button1); // 前进
        button_s = (ImageButton) findViewById(R.id.button2); // 后退
        button_a = (ImageButton) findViewById(R.id.button3); // 左转
        button_d = (ImageButton) findViewById(R.id.button4); // 右转
        button_g = (ImageButton) findViewById(R.id.button5); // 重力感应模式
 
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        /*
         * sensorManager.registerListener(listener,sensor,SensorManager.
         * SENSOR_DELAY_NORMAL);
         */
 
        button_w.setOnClickListener(this);
        button_s.setOnClickListener(this);
        button_a.setOnClickListener(this);
        button_d.setOnClickListener(this);
        button_g.setOnClickListener(this);
 
    }
 
    @Override
    public  void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1: {
            Toast.makeText(MainActivity.this, "前进", Toast.LENGTH_SHORT).show();
            break;
        }
        case R.id.button2: {
            Toast.makeText(MainActivity.this, "后退", Toast.LENGTH_SHORT).show();
            break;
        }
        case R.id.button3: {
            Toast.makeText(MainActivity.this, "左转", Toast.LENGTH_SHORT).show();
            break;
        }
        case R.id.button4: {
            Toast.makeText(MainActivity.this, "右转", Toast.LENGTH_SHORT).show();
            break;
        }
        case R.id.button5: {
            if (sensorFlag == false) {
 
                sensorFlag = true;
                Toast.makeText(MainActivity.this, "重力感应模式", Toast.LENGTH_SHORT)
                        .show();
                sensorManager.registerListener(listener, sensor,
                        SensorManager.SENSOR_DELAY_NORMAL);
            } else if (sensorFlag == true) {
                sensorFlag = false;
                Toast.makeText(MainActivity.this, "关闭重力感应", Toast.LENGTH_SHORT)
                        .show();
                sensorManager.unregisterListener(listener);// 关闭监听器
            }
        }
        default:
            break;
        }
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (sensorManager != null) {
            sensorManager.unregisterListener(listener);// 关闭监听器
        }
    }
 
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
     
    private class mySensorEventListener implements SensorEventListener{
         
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
         
         
        @Override
        public void onSensorChanged(SensorEvent event) {
             
 
            float xValue = event.values[0];
            float yValue = event.values[1];
            float zValue = event.values[2];
             
            xyz.setText("X: " + xValue + "\n" + "Y: " + yValue + "\n" + "Z: "
                    + zValue);
             
             
            if (yValue < -3) {
                if (yValue < -3) {
                    button_w.setPressed(true);
                }
            }
             
            if (yValue > 3) {
                if (yValue > 3) {
                    button_s.setPressed(true);
                }
            }
             
            if (xValue > 3) {
                if (xValue > 3) {
                    button_a.setPressed(true);
                }
            }
             
            if (xValue < -3) {
                if (xValue < -3) {
                    button_d.setPressed(true);
                }
            }
             
            if (3 > yValue && yValue > -3) {
                button_w.setPressed(false);
                button_s.setPressed(false);
            }
             
            if (3 > xValue && xValue > -3) {
                button_a.setPressed(false);
                button_d.setPressed(false);
            }
 
        }
    }
}
 


以上是关于如何在重力加速度监听器中实现button被按下的逻辑?的主要内容,如果未能解决你的问题,请参考以下文章

突出显示被按下的按钮并取消突出显示未按下的按钮 SWIFT

Firemonkey里触发home按键被按下的事件

android 键盘按键监听

通知其他观众一个按钮被按下

我试图检测某个被按下的键(Python)

如何使按钮看起来好像被按下?