0

我正在android中制作一个应用程序。当用户移动手机时,我正在尝试使用 Type_orientation 来移动屏幕上的引号。因此,当用户将手机向左移动时,下一个引号会出现在屏幕上,而当用户将手机向右移动时,前一个引号会出现在屏幕上。我设置了 25 的限制。但我的问题是,一旦手机转到位置 25,所有保存在数据库中的报价都会以幻灯片的形式出现。我只想转到下一个报价,而不是让所有报价都显示为幻灯片。我能做些什么?谁能帮我解决这个问题?

这是我的课

package com.example.prova1;

import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;



public class SlideQuote extends Activity implements SensorEventListener
{
 //a TextView
 private TextView quote;
 private TextView author;
 //the Sensor Manager
 private SensorManager sManager;
 float xLast,xCurrent;
 int id;
 int total;
 float x;
 boolean first=true;
 String s1,s2;
  Database quotedatabase = new Database(this);


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.slide_quote);

        //get the TextView from the layout file
        quote = (TextView) findViewById(R.id.textView3);
        author = (TextView) findViewById(R.id.textView4);
id=1;
        //get a hook to the sensor service

        sManager= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
     if(sManager.getSensorList(Sensor.TYPE_ORIENTATION).size()!=0){
      Sensor s =sManager.getSensorList(Sensor.TYPE_ORIENTATION).get(o);
      sManager.registerListener(this,s ,SensorManager.SENSOR_DELAY_NORMAL);
      s1= quotedatabase.getQuote(id);
      s2= quotedatabase.getAuthor(id);
      total=quotedatabase.getQuotesCount();


      quote.setText(s1);
      author.setText(s2);
    }
    }
    //when this Activity starts
    @Override
 protected void onResume()
 {
  super.onResume();
  /*register the sensor listener to listen to the gyroscope sensor, use the
  callbacks defined in this class, and gather the sensor information as quick
  as possible*/
  sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_NORMAL);
 }

  //When this Activity isn't visible anymore
 @Override
 protected void onStop()
 {
  //unregister the sensor listener
  sManager.unregisterListener(this);
  super.onStop();
 }

 @Override
 public void onAccuracyChanged(Sensor arg0, int arg1)
 {
  //Do nothing.
 }

 @Override
 public void onSensorChanged(SensorEvent event)
 {
  try {
   Thread.sleep(50);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  //if sensor is unreliable, return void
  if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
  {
   return;
  }

  if(first){
   xLast=0;   
  }
  else
  {
   xLast=xCurrent;
  }

  //else it will output the Roll, Pitch and Yawn values
  xCurrent=event.values[2];
  x=xCurrent-xLast;
  if(-1<xCurrent& xCurrent<1)
  {

  }
  else{

    if(x<=5&x>=-5){

  }
  else{
  if(x>25 ){
   if(id==total)
   {id=1;
   }
   else{

   id++;
   }
   s1= quotedatabase.getQuote(id);
      s2= quotedatabase.getAuthor(id);
      quote.setText(s1);
      author.setText(s2);
  }

      else{
  if(x<-25){
   if(id==1)
   {id=total;}
   else{
   id--;}

   s1= quotedatabase.getQuote(id);
      s2= quotedatabase.getAuthor(id);      
      quote.setText(s1);
      author.setText(s2);
  }
  }
  }
 }
  }
 @Override
 public void onBackPressed() {
  // TODO Auto-generated method stub
  sManager.unregisterListener(this);
  Intent backIntent = new Intent(getApplication(), Quote.class);
       finish();
       startActivity(backIntent);

 }


 }

我认为问题出在这部分

@Override
     public void onSensorChanged(SensorEvent event)
     {
      try {
       Thread.sleep(50);
      } catch (InterruptedException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      //if sensor is unreliable, return void
      if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
      {
       return;
      }

      if(first){
       xLast=0;   
      }
      else
      {
       xLast=xCurrent;
      }

      //else it will output the Roll, Pitch and Yawn values
      xCurrent=event.values[2];
      x=xCurrent-xLast;
      if(-1<xCurrent& xCurrent<1)
      {

      }
      else{

        if(x<=5&x>=-5){

      }
      else{
      if(x>25 ){
       if(id==total)
       {id=1;
       }
       else{

       id++;
       }
       s1= quotedatabase.getQuote(id);
          s2= quotedatabase.getAuthor(id);
          quote.setText(s1);
          author.setText(s2);
      }

          else{
      if(x<-25){
       if(id==1)
       {id=total;}
       else{
       id--;}

       s1= quotedatabase.getQuote(id);
          s2= quotedatabase.getAuthor(id);      
          quote.setText(s1);
          author.setText(s2);
      }
      }
      }
     }
      }

任何人都可以帮助我吗?

4

0 回答 0