0

日食で Android プログラムを作成しようとしています。このプログラムのアイデアは、TextViewデバイスが正確に 180 度 (xy 平面内で?) 回転した場合に、「デバイスが反転しました」というメッセージを表示することです。回転センサーを使用して、onSensorChangedイベントでコードを記述しようとしています。現在、このコードは、回転が検出されたときに TextView を変更することになっていますが、そうではありません。

だから私の質問は簡単です:

  1. 回転を指定してテキストビューを変更するにはどうすればよいですか?
  2. これを 180 度の回転に適用するにはどうすればよいですか?


package com.example.rotation2;

import android.hardware.*;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.TextView;


public class MainActivity extends Activity implements SensorEventListener {

TextView message;
private SensorManager mSensorManager;
private Sensor rotation;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main);

    TextView message = ((TextView) findViewById(R.id.message_view));


    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    rotation = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
    if (rotation != null){
      // Success! There's a rotation sensor.
        message.setText(R.string.compatible);
      }
    else {
      // Failure! No rotation sensor.
        message.setText(R.string.incompatible);
      }
}

@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) {}

@Override
public void onSensorChanged(SensorEvent event) {
    message.setText(R.string.rotated);

}


}
4

2 に答える 2

0

これを試して:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
      txtView.setText("Orientation Changed");
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        txtView.setText("Orientation Changed");
    }
}

キーボードの可視性を検出したい場合:

if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
  Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
  Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
}
于 2013-08-28T10:30:41.167 に答える
0

アクティビティでこのメソッドをオーバーライドします。

 public void onConfigurationChanged(Configuration newConfig) { 
   super.onConfigurationChanged(newConfig);
    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
       Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
       Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
  }

Androidマニフェストファイルで、アクティビティの下にこれを記載してください

android:configChanges="orientation|screenSize".
于 2013-08-28T13:10:58.177 に答える