嗨我在这里做一个应用程序。用户单击按钮一次意味着在另一个活动中我需要执行一个动作,用户再次回到同一页面点击同一个按钮意味着我需要执行不同的操作。我在activity1中有2个活动我有1个按钮,我使用活动1按钮中的一个计数变量进行treid点击那个时间我增加计数变量,使用putextra我根据计数执行动作将此计数值转换为acticity2,但计数一次是增加,如果我回到相同的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..
答案 0 :(得分:0)
在接收方创建Bundle
的对象。
Bundle myBundle = new Bundle(); // Declare above
String temp;
myBundle = getIntent().getIntExtra();
temp = myBundle.getString("ks"); // temp will have the val1.
希望你现在可以得到它。