簡體   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