[英]Do something when text it change. Java Android
I have a text with xy z. 我的文字是xy z。 Accelerometer change the values in this text.
加速度计更改此文本中的值。 I want to catch when xyz is < than -100 and say "You have xyz less than -100"
我想在xyz小于-100时说“您的xyz小于-100”
How I can do this? 我该怎么做?
My code: MainActivity.java file 我的代码: MainActivity.java文件
package com.example.acceler;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity<accelerometer> extends Activity implements SensorEventListener {
Sensor accelerometer;
SensorManager sm;
TextView acceleration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sm=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
accelerometer=sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
acceleration=(TextView)findViewById(R.id.acceleration);
}
@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 void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
acceleration.setText("X: "+event.values[0]+
"\nY:"+event.values[1]+
"\nz:"+event.values[2]);
}
}
And the xml file from layout: 以及来自布局的xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/acceleration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:text="X: Y: Z:"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
You could add some code to your onSensorChanged()
method. 您可以在
onSensorChanged()
方法中添加一些代码。 First, check the values to see if they are < -100 and if they are, set the text to whatever you like. 首先,检查值是否小于-100,如果是,则将文本设置为您喜欢的任何值。 If they aren't, then do what you already were doing.
如果不是,则执行您已经在做的事情。
@Override
public void onSensorChanged(SensorEvent event) {
if (event.values[0] < -100 || event.values[1] < -100 || event.values[2] < -100) {
acceleration.setText("You have X, Y, or Z less than -100");
}
else {
acceleration.setText("X: "+event.values[0]+
"\nY:"+event.values[1]+
"\nZ:"+event.values[2]);
}
}
You could just edit the onSensorChanged
method with an if-statement saying something like 您可以使用if语句编辑
onSensorChanged
方法,例如
if (event.values[0] < -100) { // x below limit
acceleration.setText("your x < -100" + ...);
}
Or you could do some more intricate stuff, like encapsulating some custom event and dispatching it to some custom mechanism for dealing with it (you need to write both). 或者,您可以做一些更复杂的事情,例如封装一些自定义事件并将其分派到某种自定义机制中进行处理(您需要同时编写两者)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.