旋转设备时如何更改TextView?

时间:2013-08-28 10:23:16

标签: android rotation sensor

我正在尝试在eclipse中编写一个Android程序。该程序的想法是在TextView中显示一条消息,说“设备已被翻转”,如果设备正好旋转180度(我认为在xy平面上?)。我正在使用旋转传感器并尝试在onSensorChanged事件中编写代码。目前,当检测到任何旋转时,此代码应该更改TextView,但它不会。

所以我的问题很简单:

  1. 如果轮换,如何更改textview?
  2. 如何将此应用于180度的旋转?

  3. 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);
    
    }
    
    
    }
    

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:0)

在Activity中覆盖此方法:

 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 Manifest文件中的

在活动

下提及此内容
android:configChanges="orientation|screenSize".
相关问题