|
|
|
Android - จะอ้างถึง (TextView) findViewById จากคลาสอื่นที่ไม่ได้ extends class Activity ได้อย่างไรค่ะ |
|
|
|
|
|
|
|
Code (Android-Java)
View v = getLayoutInflater().inflate(R.layout.mylayout, null);
TextView tv = (TextView )v.findViewById(R.id.tv1);
ของผมใช้ findviewตามlayout
ผิดพลาดประการใดขออภัยครับ
|
|
|
|
|
Date :
2013-08-26 16:09:38 |
By :
zFOLKz |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ประมาณนั้นแหละครับ จะต้องอ้างถึงไฟล์ Layout ซะก่อน
|
|
|
|
|
Date :
2013-08-26 16:37:44 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตอบความคิดเห็นที่ : 2 เขียนโดย : mr.win เมื่อวันที่ 2013-08-26 16:37:44
รายละเอียดของการตอบ ::
รบกวนช่วยแสดงค่าองศา(azimuth) ใน (Textview) ได้ไหมค่ะ
Code (Android-Java)
package com.iutinvg.compass;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
public class Compass implements SensorEventListener {
private static final String TAG = "Compass";
private SensorManager sensorManager;
private Sensor gsensor;
private Sensor msensor;
private float[] mGravity = new float[3];
private float[] mGeomagnetic = new float[3];
private float azimuth = 0f;
private float currectAzimuth = 0;
// compass arrow to rotate
public ImageView arrowView = null;
TextView tv;
public Compass(Context context) {
sensorManager = (SensorManager) context
.getSystemService(android.content.Context.SENSOR_SERVICE);
gsensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
msensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View v = inflater.inflate( R.layout.activity_compass, null );
tv = (TextView )v.findViewById(R.id.textView1);
}
public void start() {
sensorManager.registerListener(this, gsensor,
SensorManager.SENSOR_DELAY_GAME);
sensorManager.registerListener(this, msensor,
SensorManager.SENSOR_DELAY_GAME);
}
public void stop() {
sensorManager.unregisterListener(this);
}
private void adjustArrow() {
if (arrowView == null) {
Log.i(TAG, "arrow view is not set");
return;
}
Log.i(TAG, "will set rotation from " + currectAzimuth + " to "
+ azimuth);
Animation an = new RotateAnimation(-currectAzimuth, -azimuth,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
currectAzimuth = azimuth;
an.setDuration(500);
an.setRepeatCount(0);
an.setFillAfter(true);
arrowView.startAnimation(an);
}
@Override
public void onSensorChanged(SensorEvent event) {
final float alpha = 0.97f;
synchronized (this) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
mGravity[0] = alpha * mGravity[0] + (1 - alpha)
* event.values[0];
mGravity[1] = alpha * mGravity[1] + (1 - alpha)
* event.values[1];
mGravity[2] = alpha * mGravity[2] + (1 - alpha)
* event.values[2];
// mGravity = event.values;
// Log.e(TAG, Float.toString(mGravity[0]));
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
// mGeomagnetic = event.values;
mGeomagnetic[0] = alpha * mGeomagnetic[0] + (1 - alpha)
* event.values[0];
mGeomagnetic[1] = alpha * mGeomagnetic[1] + (1 - alpha)
* event.values[1];
mGeomagnetic[2] = alpha * mGeomagnetic[2] + (1 - alpha)
* event.values[2];
// Log.e(TAG, Float.toString(event.values[0]));
}
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity,
mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
// Log.d(TAG, "azimuth (rad): " + azimuth);
azimuth = (float) Math.toDegrees(orientation[0]); // orientation
azimuth = (azimuth + 360) % 360;
// Log.d(TAG, "azimuth (deg): " + azimuth);
adjustArrow();
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
|
|
|
|
|
Date :
2013-08-26 16:45:14 |
By :
chaaim2you |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (Android-Java)
tv.setText(azimuth);
|
|
|
|
|
Date :
2013-08-26 17:07:32 |
By :
zFOLKz |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|