简体   繁体   中英

Do something when text it change. Java Android

I have a text with 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"

How I can do this?

My code: MainActivity.java file

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:

<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. First, check the values to see if they are < -100 and if they are, set the text to whatever you like. 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 (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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM