2012-04-27 51 views
0

您好我正在做一个应用程序here.user单击按钮一次意味着在另一个活动中我需要执行一个动作,用户再次回到同一页面agian单击相同的按钮意味着我需要执行不同的操作。我在活动1采取2个活动我有1个按钮,我treid在activity1按钮中使用一个计数变量按钮单击那次我递增计数变量,使用putextra我将计数值转换为acticity2这里根据计数我执行动作,但计数一次是随着时间的推移,如果我回到相同的activty1然后我点击按钮,也计数是'1',只是,它不是2 .so如何解决这个问题任何有想法的人建议我。如何将一个类的透明变量值转换为另一个类android

Activity1 .class: 
public class Activity1 extends Activity implements OnClickListener{ 
Button b1,b2; 
    int countk4=0; 

    SharedPreferences share; 
    String bol3; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.home1); 
    b1=(Button)findViewById(R.id.button1); 
    b1.setOnClickListener(this); 
    } 

public void onClick(View v) { 
    // TODO Auto-generated method stub 
    if(v==b1) 
    { 
     countk4++; 
     share=getSharedPreferences("shared", MODE_WORLD_WRITEABLE); 
     SharedPreferences.Editor editor=share.edit(); 

     editor.putInt("roll", countk4); 

     editor.commit(); 

     show(); 
     Intent i=new Intent(Activity1 .this,Activity2.class); 
     i.putExtra("k", 1); 
     i.putExtra("k1", 13); 
     i.putExtra("ks", val1); 
     startActivity(i); 
    } 
} 
Integer val1,val2; 
private void show() { 
    // TODO Auto-generated method stub 
    share=getSharedPreferences("shared", MODE_WORLD_READABLE); 

    val1=share.getInt("roll",0); 

    } 
     } 

    Activity2 .class: 
    public class Activity2 extends Activity implements OnClickListener{ 
Button back; 
     int countkv; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.home2); 
    back=(Button)findViewById(R.id.button1); 
    back.setOnClickListener(this); 
      countkv = getIntent().getIntExtra("ks", 0); 

    if(countkv =1) 
    { 
    Toast.makeText(getApplicationContext(), "hai.......i am first", Toast.LENGTH_LONG).show(); 
    } 

if(countkv =2) 
{ 
    Toast.makeText(getApplicationContext(), "hai.......1 am second", Toast.LENGTH_LONG).show(); 
    } 

    public void onClick(View v) { 
    // TODO Auto-generated method stub 
    if(v==back) 
    { 
     Intent i=new Intent(Activity2.this,Activity1.class); 


     startActivity(i); 
    } 


} 
} 
} 
but above every time first toast message displayed becz countkv is every time 1 only.. 

回答

0

在收货方作出Bundle的对象。

Bundle myBundle = new Bundle(); // Declare above 
    String temp; 

    myBundle = getIntent().getIntExtra(); 
    temp = myBundle.getString("ks"); // temp will have the val1. 

希望你能现在得到它。

+0

查询您的回应,在这里我从ativity1获取值到activity2 prblm是如果用户再次返回activity2到activity1时间计数变量refereshed,使计数'0'的时间在那里,'1'删除所以如果我再点击一下按钮,count也增加到1,而不是2.那是我的prblm – user1089640 2012-04-27 11:33:14

+0

@ user1089640:然后声明你的变量为Public Static修饰符。试试这个链接 - > http://www.roseindia.net/java/beginners/howtoaccessstaticmethod.shtml – Bhavin 2012-04-27 11:56:50

+0

thakyou这么多我使用公共int countk4时间我不getting.but现在我使用公共静态int countk4现在我越来越结果 – user1089640 2012-04-27 12:11:29

相关问题