作者:个信2502907653 | 来源:互联网 | 2024-11-17 07:45
------------------------MainActivity.java---------------------------------
package com.example.slider;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sp = getSharedPreferences("config", MODE_PRIVATE);
boolean isCOnfigured= sp.getBoolean("isConfigured", false);
if (isConfigured) {
setContentView(R.layout.activity_main);
} else {
Intent intent = new Intent(this, Setup1Activity.class);
startActivity(intent);
finish();
}
}
public void reEnterSetup(View view) {
Intent intent = new Intent(this, Setup1Activity.class);
startActivity(intent);
finish();
}
}
--------------------------------BaseSetupActivity.java---------是下面3个activity 的父类------------------
package com.example.slider;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public abstract class BaseSetupActivity extends Activity {
private GestureDetector gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gestureDetector = new GestureDetector(this, new SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (Math.abs(velocityX) <200) {
Toast.makeText(getApplicationContext(), "滑动速度太慢", Toast.LENGTH_SHORT).show();
return true;
}
if (Math.abs(e2.getRawY() - e1.getRawY()) > 100) {
Toast.makeText(getApplicationContext(), "请水平滑动", Toast.LENGTH_SHORT).show();
return true;
}
if ((e2.getRawX() - e1.getRawX()) > 200) {
showPreviousPage();
return true;
}
if ((e1.getRawX() - e2.getRawX()) > 200) {
showNextPage();
return true;
}
return super.onFling(e1, e2, velocityX, velocityY);
});
}
public abstract void showNextPage();
public abstract void showPreviousPage();
public void onNextClick(View view) {
showNextPage();
}
public void onPreviousClick(View view) {
showPreviousPage();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
}
-------------------------------Setup1Activity.java------------------------
package com.example.slider;
import android.content.Intent;
import android.os.Bundle;
public class Setup1Activity extends BaseSetupActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup1);
}
@Override
public void showNextPage() {
Intent intent = new Intent(this, Setup2Activity.class);
startActivity(intent);
finish();
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
@Override
public void showPreviousPage() {
}
}
------------------------Setup2Activity.java---------------------
package com.example.slider;
import android.content.Intent;
import android.os.Bundle;
public class Setup2Activity extends BaseSetupActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup2);
}
@Override
public void showNextPage() {
Intent intent = new Intent(this, Setup3Activity.class);
startActivity(intent);
finish();
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
@Override
public void showPreviousPage() {
Intent intent = new Intent(this, Setup1Activity.class);
startActivity(intent);
finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
}
---------------------Setup3Activity.java---------------------
package com.example.slider;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
public class Setup3Activity extends BaseSetupActivity {
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup3);
sp = getSharedPreferences("config", MODE_PRIVATE);
}
@Override
public void showNextPage() {
Editor editor = sp.edit();
editor.putBoolean("isConfigured", true);
editor.apply();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
@Override
public void showPreviousPage() {
Intent intent = new Intent(this, Setup2Activity.class);
startActivity(intent);
finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
}
。。。。。。。。main.xml。。。。。。。。。。。。。。。。
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">