Use PageHelper to handle page change

This commit is contained in:
icewithcola 2024-04-16 10:29:33 +08:00
parent 26f02ad119
commit 17386d5b04
5 changed files with 62 additions and 32 deletions

View file

@ -20,7 +20,7 @@ import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
PageHelper pageHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -49,16 +49,15 @@ public class MainActivity extends AppCompatActivity {
}
textViewAppendString(tv,"*************************\nfinished");
// Initialize Page Helper
pageHelper = new PageHelper(this,null,MainActivity2.class);
Button button = findViewById(R.id.Page1NextPage);
button.setEnabled(true);
}
public void jumpToNext(View view) {
Button button = findViewById(R.id.Page1NextPage);
button.setEnabled(true);
Intent myIntent = new Intent(this, MainActivity2.class);
startActivity(myIntent);
pageHelper.goNext();
}
private void textViewAppendString(@NonNull TextView tv, String s){

View file

@ -20,6 +20,8 @@ import androidx.core.view.WindowInsetsCompat;
public class MainActivity2 extends AppCompatActivity {
PageHelper pageHelper;
int _text_size = 70;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -31,6 +33,7 @@ public class MainActivity2 extends AppCompatActivity {
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
pageHelper = new PageHelper(this,MainActivity.class,Page3.class);
}
@Override
@ -59,9 +62,9 @@ public class MainActivity2 extends AppCompatActivity {
public void setColor(View view) {
TextView t = findViewById(R.id.test2strview);
EditText r_edit = (EditText) findViewById(R.id.R);
EditText g_edit = (EditText) findViewById(R.id.G);
EditText b_edit = (EditText) findViewById(R.id.B);
EditText r_edit = findViewById(R.id.R);
EditText g_edit = findViewById(R.id.G);
EditText b_edit = findViewById(R.id.B);
int _r,_g,_b;
// Here we are testing toast
@ -92,7 +95,7 @@ public class MainActivity2 extends AppCompatActivity {
public void setText(View view) {
TextView textView = findViewById(R.id.test2strview);
EditText editText = findViewById(R.id.Page2SetTestText);
String s = "Test Text";
String s;
try {
s = editText.getText().toString();
}catch (Exception e){
@ -102,16 +105,6 @@ public class MainActivity2 extends AppCompatActivity {
textView.setText(s);
}
public void goNext() {
Intent myIntent = new Intent(this, Page3.class);
startActivity(myIntent);
}
public void goPrev() {
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
}
public void setSize(View view) {
Button sp = findViewById(R.id.Page2SetSP);
Button dp = findViewById(R.id.Page2SetDP);
@ -135,7 +128,7 @@ public class MainActivity2 extends AppCompatActivity {
{
@Override
public void onClick(View v){
goNext();
pageHelper.goNext();
}
}
@ -143,7 +136,7 @@ public class MainActivity2 extends AppCompatActivity {
{
@Override
public boolean onLongClick(View v){
goPrev();
pageHelper.goPrev();
return false;
}
}

View file

@ -16,6 +16,8 @@ import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class Page3 extends AppCompatActivity {
PageHelper pageHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -26,6 +28,9 @@ public class Page3 extends AppCompatActivity {
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
pageHelper = new PageHelper(this,MainActivity2.class,Page4.class);
findViewById(R.id.P3_0).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3_1).setOnClickListener(new CalculateOnClickListener());
findViewById(R.id.P3_2).setOnClickListener(new CalculateOnClickListener());
@ -69,6 +74,7 @@ public class Page3 extends AppCompatActivity {
s = "";
dot.setEnabled(true);
setOpButton(true);
findViewById(R.id.P3EqualButton).setEnabled(true);
}else {
s = s.substring(0, s.length() - 1);
dot.setEnabled(!isNumberWithDot(s));
@ -79,12 +85,16 @@ public class Page3 extends AppCompatActivity {
case "CE":
s = "";
dot.setEnabled(true);
setOpButton(true);
findViewById(R.id.P3EqualButton).setEnabled(true);
break;
case "=":
calc();
s = tv.getText().toString();
dot.setEnabled(!s.contains("."));
setOpButton(true);
findViewById(R.id.P3EqualButton).setEnabled(
!s.toLowerCase().contains("n")); // For Inf & NaN
return;
case ".":
dot.setEnabled(false);
@ -152,20 +162,11 @@ public class Page3 extends AppCompatActivity {
textView.setText(result);
}
public void goNext() {
Intent myIntent = new Intent(this, Page4.class);
startActivity(myIntent);
}
public void goPrev() {
Intent myIntent = new Intent(this, MainActivity2.class);
startActivity(myIntent);
}
final class PageButtonHandler implements View.OnClickListener
{
@Override
public void onClick(View v){
goNext();
pageHelper.goNext();
}
}
@ -173,7 +174,7 @@ public class Page3 extends AppCompatActivity {
{
@Override
public boolean onLongClick(View v){
goPrev();
pageHelper.goPrev();
return false;
}
}

View file

@ -21,4 +21,7 @@ public class Page4 extends AppCompatActivity {
return insets;
});
}
}

View file

@ -0,0 +1,34 @@
package uk.kagurach.android101;
import static androidx.core.content.ContextCompat.startActivity;
import android.content.Context;
import android.content.Intent;
public class PageHelper {
private final Context _curr;
private final Class<?> _prev;
private final Class<?> _next;
PageHelper(Context curr,Class<?> prev,Class<?> next){
_curr = curr;
_prev = prev;
_next = next;
}
void goPrev(){
if (_prev==null){
return;
}
Intent myIntent = new Intent(_curr, _prev);
startActivity(_curr,myIntent,null);
}
void goNext(){
if (_next==null){
return;
}
Intent myIntent = new Intent(_curr, _next);
startActivity(_curr,myIntent,null);
}
}