gpt4 book ai didi

java - 单击按钮时激活接近传感器

转载 作者:行者123 更新时间:2023-12-01 11:24:43 25 4
gpt4 key购买 nike

我尝试仅在单击按钮时注册接近传感器监听器。但似乎什么也没有发生。我曾尝试在互联网上探索类似的东西,但什么也没得到。我究竟做错了什么?请帮忙!

xml 文件:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/pushupbg"
android:orientation="vertical" >


<TextView
android:id="@+id/tvTitlePU"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Push Up Counter"
android:textColor="#98f5ff"
android:textSize="25sp"
android:padding="10sp"
android:textAppearance="?android:attr/textAppearanceLarge" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/fragment1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:orientation="horizontal" >

<Button
android:id="@+id/bStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:typeface="serif"
android:onClick="startCounter"
android:text="Start" />

<Button
android:id="@+id/bStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:typeface="serif"
android:onClick="stopCounter"
android:text="Stop" />
<Button
android:id="@+id/bSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:typeface="serif"
android:onClick="saveData"
android:text="Save" />


</LinearLayout>

<TextView
android:id="@+id/tvCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvTitlePU"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text=""
android:textColor="#ffffff"
android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

这是java文件:

public class PushUp_Counter extends Activity implements SensorEventListener {
SensorManager smgr;
Sensor sensor;
Button start,stop,save;
TextView title,count;
int counter=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pushup_layout);
start=(Button)findViewById(R.id.bStart);
stop=(Button)findViewById(R.id.bStop);
save=(Button)findViewById(R.id.bSave);
title=(TextView)findViewById(R.id.tvTitlePU);
count=(TextView)findViewById(R.id.tvCount);
stop.setEnabled(false);
save.setEnabled(false);

}


public void startCounter(View v) {
// TODO Auto-generated method stub
smgr=(SensorManager)getSystemService(SENSOR_SERVICE);
sensor=smgr.getDefaultSensor(Sensor.TYPE_PROXIMITY);
smgr.registerListener(this, sensor,SensorManager.SENSOR_DELAY_NORMAL);
stop.setEnabled(true);
}

public void stopCounter(View v) {
// TODO Auto-generated method stub
smgr.unregisterListener(this);
save.setEnabled(true);
}

public void saveData(View v) {
// TODO Auto-generated method stub

}

@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent arg0) {
// TODO Auto-generated method stub
if (arg0.values[0] == 0){
counter++;
count.setText("Your count is: "+counter);
}
}
}

日志猫:

06-19 00:01:21.780: W/EGL_genymotion(1493): eglSurfaceAttrib not implemented
06-19 00:01:29.396: I/ActivityManager(401): START u0 {cmp=com.example.fitgo/.PushUp_Counter} from pid 1493
06-19 00:01:29.412: W/genymotion_audio(125): out_write() limiting sleep time 44149 to 23219
06-19 00:01:29.452: W/genymotion_audio(125): out_write() limiting sleep time 55759 to 23219
06-19 00:01:29.472: W/genymotion_audio(125): out_write() limiting sleep time 67369 to 23219
06-19 00:01:29.504: W/genymotion_audio(125): out_write() limiting sleep time 58979 to 23219
06-19 00:01:29.524: W/genymotion_audio(125): out_write() limiting sleep time 40589 to 23219
06-19 00:01:29.548: W/genymotion_audio(125): out_write() limiting sleep time 32199 to 23219
06-19 00:01:29.572: W/genymotion_audio(125): out_write() limiting sleep time 23809 to 23219
06-19 00:01:29.856: D/dalvikvm(401): GC_FOR_ALLOC freed 441K, 17% free 13865K/16596K, paused 25ms, total 25ms
06-19 00:01:29.884: D/dalvikvm(401): GC_FOR_ALLOC freed 748K, 19% free 13487K/16596K, paused 22ms, total 22ms
06-19 00:01:29.884: D/MobileDataStateTracker(401): default: setPolicyDataEnable(enabled=true)
06-19 00:01:30.152: W/EGL_genymotion(1493): eglSurfaceAttrib not implemented
06-19 00:01:30.360: I/ActivityManager(401): Displayed com.example.fitgo/.PushUp_Counter: +458ms
06-19 00:01:34.372: W/genymotion_audio(125): out_write() limiting sleep time 44149 to 23219
06-19 00:01:34.396: W/genymotion_audio(125): out_write() limiting sleep time 55759 to 23219
06-19 00:01:34.424: W/genymotion_audio(125): out_write() limiting sleep time 47369 to 23219
06-19 00:01:34.448: W/genymotion_audio(125): out_write() limiting sleep time 38979 to 23219
06-19 00:01:34.492: W/genymotion_audio(125): out_write() limiting sleep time 31178 to 23219
06-19 00:01:38.448: W/genymotion_audio(125): out_write() limiting sleep time 44149 to 23219
06-19 00:01:38.468: W/genymotion_audio(125): out_write() limiting sleep time 55759 to 23219
06-19 00:01:38.496: W/genymotion_audio(125): out_write() limiting sleep time 47369 to 23219
06-19 00:01:38.516: W/genymotion_audio(125): out_write() limiting sleep time 38979 to 23219
06-19 00:01:38.564: W/genymotion_audio(125): out_write() limiting sleep time 31178 to 23219

最佳答案

像这样添加 onClickListener start.setOnClickListener(startCounter)

或者你也可以这样做

bStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startCounter(v);
}
});

编辑

让我们对您的代码进行更改,看看它是否与您的代码有关。

首先删除这行代码:

android:onClick="startCounter"

然后在您的 PushUp_Counter 类上将标题更改为:

public class PushUp_Counter extends Activity implements SensorEventListener, OnClickListener  {

您必须在 onCreate() 中添加这行代码:

start.setOnClickListener(this);

然后你必须添加方法onClick(),如下所示:

@Override
public void onClick(View v) {
// getId() returns this view's identifier.
switch(v.getId()){
case R.id.bStart;
// TODO Auto-generated method stub
smgr=(SensorManager)getSystemService(SENSOR_SERVICE);
sensor=smgr.getDefaultSensor(Sensor.TYPE_PROXIMITY);
smgr.registerListener(this, sensor,SensorManager.SENSOR_DELAY_NORMAL);
stop.setEnabled(true);
break;
}
}

如果代码的其余部分出现任何错误,请对其余代码进行注释,只需尝试此按钮并告诉我它是否有效。

关于java - 单击按钮时激活接近传感器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30922312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com