繁体   English   中英

文本更改时,请执行某些操作。 Java安卓

[英]Do something when text it change. Java Android

我的文字是xy z。 加速度计更改此文本中的值。 我想在xyz小于-100时说“您的xyz小于-100”

我该怎么做?

我的代码: 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]);
}
}

以及来自布局的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>

您可以在onSensorChanged()方法中添加一些代码。 首先,检查值是否小于-100,如果是,则将文本设置为您喜欢的任何值。 如果不是,则执行您已经在做的事情。

@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]);
    }
}

您可以使用if语句编辑onSensorChanged方法,例如

if (event.values[0] < -100) { // x below limit
    acceleration.setText("your x < -100" + ...);
}

或者,您可以做一些更复杂的事情,例如封装一些自定义事件并将其分派到某种自定义机制中进行处理(您需要同时编写两者)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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