gpt4 book ai didi

java - 在滑动 Activity 和维护数据之间切换

转载 作者:行者123 更新时间:2023-11-29 03:23:44 26 4
gpt4 key购买 nike

我有 3 个 Activity Add_Case1、Add_Case2 和 Add_Case3。我正在使用滑动手势在它们之间来回切换。

我的问题是:我在 Add_Case1我填写了一些数据,然后滑动到 Add_Case2。现在在 Activity 中填写数据Add_Case2然后滑动到 Add_Case3现在 Activity ,如果我向后滑动以转到 Add_Case1Add_Case2 ,我可以看到我填写的数据仍然存在。但是当我现在再次从 Add_Case1 滑动时至 Add_Case2Add_Case3 .数据已丢失。

我尝试使用 android:launchMode="singleTop" , android:launchMode="singleTask" ,甚至 onSaveInstanceState 但它不起作用。
这是文件的相关代码

Add_Case1

            public class Add_case1 extends Activity implements SimpleGestureListener{
private SimpleGestureFilter detector;
public Spinner et5;
public EditText et1,et2,et3,et4;
public static String s1,s2,s3,s4,s5,s6[]; //variables defined as static so that it can be used in
// Add_case3.java directly
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Oncreate()","Add Case 1");
if(savedInstanceState!=null)
{
Log.d("Oncreate() sis","Add Case 1");
et1.setText(savedInstanceState.getString("et1"));
et2.setText(savedInstanceState.getString("et2"));
et3.setText(savedInstanceState.getString("et3"));
et4.setText(savedInstanceState.getString("et4"));
}
setContentView(R.layout.activity_add_case);
et1=(EditText)findViewById(R.id.editText1);
et2=(EditText)findViewById(R.id.editText2);
et3=(EditText)findViewById(R.id.editText3);
et4=(EditText)findViewById(R.id.editText4);
et5=(Spinner)findViewById(R.id.case_stage);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.case_stages, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
et5.setAdapter(adapter);
et5.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int pos, long arg3)
{
int index = arg0.getSelectedItemPosition();
s6=getResources().getStringArray(R.array.case_stages);
s5=s6[index];
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});
// Detect touched area
detector = new SimpleGestureFilter(this,this);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// getIntent() should always return the most recent
setIntent(intent);
}



@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(savedInstanceState);
Log.d("OnSaveInstanceState()","Add Case 1");
savedInstanceState.putString("et1", et1.getText().toString());
savedInstanceState.putString("et2", et2.getText().toString());
savedInstanceState.putString("et3", et3.getText().toString());
savedInstanceState.putString("et4", et4.getText().toString());

}

@Override
public boolean dispatchTouchEvent(MotionEvent me){
// Call onTouchEvent of SimpleGestureFilter class
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
@Override
public void onSwipe(int direction) {
//assigning the values entered by user in static data members
s1=et1.getText().toString();
s2=et2.getText().toString();
s3=et3.getText().toString();
s4=et4.getText().toString();
switch (direction) {

case SimpleGestureFilter.SWIPE_LEFT :
Intent openStartingPoint=new Intent("app.schedulawyer.com.Add_case2");
openStartingPoint.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
openStartingPoint.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(openStartingPoint);
//finish();
// break;
}
//Toast.makeText(this, "Left Swipe", Toast.LENGTH_SHORT).show();
}

@Override
public void onDoubleTap() {
// TODO Auto-generated method stub

}
}

Add_Case2

        public class Add_case2 extends FragmentActivity implements SimpleGestureListener{
private SimpleGestureFilter detector;
public EditText et6,et7,et8;
DateDialogFragment fragnd,fragpd;
Calendar nownd,nowpd;
TextView date,date_pr;
int a;
public static String s6,s7,s8,s9,s10; //static strings for direct and easy use in Add_case3.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Oncreate()","Add Case 2");
if(savedInstanceState!=null)
{
Log.d("Oncreate() sis","Add Case 2");
et6.setText(savedInstanceState.getString("et6"));
et7.setText(savedInstanceState.getString("et7"));
et8.setText(savedInstanceState.getString("et8"));

}
setContentView(R.layout.activity_add_case2);
et6=(EditText)findViewById(R.id.editText1);
et7=(EditText)findViewById(R.id.editText2);
et8=(EditText)findViewById(R.id.editText3);
nownd = Calendar.getInstance();
nowpd=nownd;
date_pr = (TextView)findViewById(R.id.textView4);
date = (TextView)findViewById(R.id.date_picker);
date_pr.setPaintFlags(date_pr.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
date.setPaintFlags(date.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
date_pr.setText(String.valueOf(nownd.get(Calendar.DAY_OF_MONTH))+"-"+String.valueOf(nownd.get(Calendar.MONTH)+1)+"-"+String.valueOf(nownd.get(Calendar.YEAR)));
date.setText(String.valueOf(nownd.get(Calendar.DAY_OF_MONTH))+"-"+String.valueOf(nowpd.get(Calendar.MONTH)+1)+"-"+String.valueOf(nowpd.get(Calendar.YEAR)));

date_pr.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
a=1;
showDialog();
}
});

date.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
a=2;
showDialog();
}

});

// Detect touched area
detector = new SimpleGestureFilter(this,this);
}
public void showDialog() {
if(a==1)
{
fragnd = DateDialogFragment.newInstance(this, new DateDialogFragmentListener(){
public void updateChangedDate(int year, int month, int day){
date_pr.setText(String.valueOf(day)+"-"+String.valueOf(month+1)+"-"+String.valueOf(year));
nownd.set(year, month, day);
// s6=String.valueOf(day)+"-"+String.valueOf(month+1)+"-"+String.valueOf(year);
Log.d("date picker","Showing up from tv1 next date ="+s6);
}
}, nownd);
fragnd.show(getSupportFragmentManager(), "DateDialogFragment");
}
else if(a==2)
{
fragpd = DateDialogFragment.newInstance(this, new DateDialogFragmentListener(){
public void updateChangedDate(int year, int month, int day){

date.setText(String.valueOf(day)+"-"+String.valueOf(month+1)+"-"+String.valueOf(year));
nowpd.set(year, month, day);
// s7=String.valueOf(day)+"-"+String.valueOf(month)+"-"+String.valueOf(year);
Log.d("date picker","Showing up from textView s7 ="+s7); }
}, nowpd);
fragpd.show(getSupportFragmentManager(), "DateDialogFragment");
}

}
public interface DateDialogFragmentListener{
//this interface is a listener between the Date Dialog fragment and the activity to update the textview's date
public void updateChangedDate(int year, int month, int day);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// getIntent() should always return the most recent
setIntent(intent);
}
@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(savedInstanceState);
Log.d("OnSaveInstanceState()","Add Case 2");
savedInstanceState.putString("et6", et6.getText().toString());
savedInstanceState.putString("et7", et7.getText().toString());
savedInstanceState.putString("et8", et8.getText().toString());

}
@Override
public boolean dispatchTouchEvent(MotionEvent me){
// Call onTouchEvent of SimpleGestureFilter class
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
@Override
public void onSwipe(int direction) {
s6= date_pr.getText().toString();
s7= date.getText().toString();
s8=et8.getText().toString(); //stores remarks
s9=et6.getText().toString(); //stores fee settled
s10=et7.getText().toString(); //stores fee paid

switch (direction) {

case SimpleGestureFilter.SWIPE_RIGHT :
Intent openStartingPoint=new Intent("app.schedulawyer.com.Add_case1");
openStartingPoint.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
openStartingPoint.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(openStartingPoint);
//finish();
break;
case SimpleGestureFilter.SWIPE_LEFT :
Intent openStartingPoint2=new Intent("app.schedulawyer.com.Add_case3");
openStartingPoint2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
openStartingPoint2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(openStartingPoint2);
//finish();
break;

}
}

@Override
public void onDoubleTap() {

}

}

Add_Case3

        public class Add_case3 extends Activity implements SimpleGestureListener{
private SimpleGestureFilter detector;
Button but;
EditText et9,et10,et11;



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_case3);
final DatabaseHandler dbs = new DatabaseHandler(this);
but=(Button)findViewById(R.id.Sumit);
et9=(EditText)findViewById(R.id.editText1);
et10=(EditText)findViewById(R.id.editText2);
et11=(EditText)findViewById(R.id.editText3);


but.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//inserting Add_case1 data in case_info table
Log.d("Insert case: ", "Inserting case..");
dbs.addCase(new Case(Add_case1.s1,Add_case1.s2,Add_case1.s3,Add_case1.s4,Add_case1.s5));

//inserting Add_case2 data in date_info table
Log.d("Insert date: ", "Inserting date..");
dbs.addDate(new Date(Add_case1.s2,Add_case2.s6,Add_case2.s7,Add_case2.s9,Add_case2.s10,Add_case2.s8));

//inserting Add_case3 data in client_info table
Log.d("Insert client: ", "Inserting client..");
dbs.addClient(new Client(Add_case1.s2,et9.getText().toString(),et10.getText().toString(),et11.getText().toString()));
Log.d("Insert: ", "Inserting finish..");


Intent openStartingPoint=new Intent("app.schedulawyer.com.View");
Log.d("view: ", "starting activity view ..");
startActivity(openStartingPoint);
finish();

}
});

// Detect touched area
detector = new SimpleGestureFilter(this,this);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// getIntent() should always return the most recent
setIntent(intent);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}

@Override
public boolean dispatchTouchEvent(MotionEvent me){
// Call onTouchEvent of SimpleGestureFilter class
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
@Override
public void onSwipe(int direction) {

switch (direction) {

case SimpleGestureFilter.SWIPE_RIGHT :
Intent openStartingPoint=new Intent("app.schedulawyer.com.Add_case2");
openStartingPoint.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
openStartingPoint.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(openStartingPoint);
//finish();
break;

}
// CharSequence str="";
//Toast.makeText(this, "Right Swipe", Toast.LENGTH_SHORT).show();
}

@Override
public void onDoubleTap() {
// Toast.makeText(this, "Double Tap", Toast.LENGTH_SHORT).show();
}

}

list

<activity
android:name="app.schedulawyer.com.Add_case1"
android:label="Add Case"
android:screenOrientation="portrait"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="app.schedulawyer.com.Add_case1" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="app.schedulawyer.com.Add_case2"
android:label="Add Case"
android:screenOrientation="portrait"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="app.schedulawyer.com.Add_case2" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="app.schedulawyer.com.Add_case3"
android:label="Add Case"
android:screenOrientation="portrait"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="app.schedulawyer.com.Add_case3" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

最佳答案

我认为您应该彻底改变执行此操作的方式。如果你真的想使用 Activity ,你可以将数据保存到磁盘或数据库,或者如果它是小的 SharedPreferences。这样你就可以随时获取数据。

但我认为您应该在其中使用 1 个 Activity、1 个 ViewPager 以及在 ViewPager 中使用 3 个 Fragments .

您可以在下面找到教程: http://developer.android.com/training/animation/screen-slide.html

您还可以在打开新 Activity 时准备好代码,只需使用“新建空白 Activity ”,然后选择“滑动 View + 标题栏”作为导航类型。之后,您将拥有我上面描述的行为,然后您可以根据需要删除 Title Strip。

关于java - 在滑动 Activity 和维护数据之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22166259/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com