嗨,我在这里做一个应用程序。用户单击按钮一次意味着在另一个活动中我需要执行一个操作,用户再次返回同一页面,再次单击同一按钮意味着我需要执行不同的操作。我在活动 1 中有 2 个活动我有 1 个按钮,我在活动 1 按钮中使用一个计数变量单击那个时间我增加计数变量,使用 putextra 我根据计数将计数值转换为活动 2 我执行操作,但计数一次是在增加时,如果我回到相同的 activty1,然后我单击按钮,计数也仅为 '1',它不会增加到 2.so 如何解决这个问题任何有想法的人建议我。
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..