如何通过意图接收int [英] How to receive an int through an Intent
问题描述
我正在通过一个Intent传递一个int,但是我不知道如何接收它,因为我必须从OnCreate方法中接收一个intent,但是如果我将其放置在那里,则无法将其与其他Int进行比较.代码: 我在这里发送意图:
I am passing an int through an Intent but I do not know how to receive it because I have to receive an intent from the OnCreate method but if I place it there I can't compare it to another int in the rest of the code: Here I am sending the Intent:
public class HomeActivityPro extends ActionBarActivity {
EditText conttext = (EditText) findViewById ( R.id.texthome );
Button buttone = (Button) findViewById(R.id.buttone);
String maxom = conttext.getText().toString();
int maxam = Integer.parseInt(maxom);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_home);
View.OnClickListener maxim = new View.OnClickListener() {
@Override
public void onClick (View view) {
Intent wall = new Intent(HomeActivityPro.this, GuessOne.class);
wall.putExtra("maxPressed", maxam);
startActivity(wall);
}
};
buttone.setOnClickListener(maxim);
我在这里收到它:
public class GuessOne extends ActionBarActivity {
int randone;
int contone;
Bundle bundle;
int maxnumpre = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_guess_one);
Intent wall = getIntent();
int maxnumpre = wall.getIntExtra("maxPressed", 0);}
但是在onCreate方法之后,我必须这样做:
But after the onCreate method I have to do this:
if (contone >= maxnumpre ){
resultaone.setText("You Failed" + " " + maxnumpre);
Toast.makeText(this, "You Failed", Toast.LENGTH_LONG).show();
}
推荐答案
您需要获取不在声明中的onCreate
方法内部传递的数据.
You need to get the data passed inside onCreate
method not in the declaration.
此外,您没有发送Bundle
,而是在Intent
中发送了String
.因此,您需要从Intent
中获取String
.
Also you are not sending a Bundle
, you are sending a String
in the Intent
. So you need to get the String
from the Intent
.
像这样
public class GuessOne extends ActionBarActivity {
int randone;
int contone;
Bundle bundle;
String maxPressed = "";
int maxcont = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_guess_one);
maxPressed = getIntent().getStringExtra("maxNumberPressed");
try {
maxCont = Integer.parseInt(maxPressed);
}
catch (NumberFormatException e) {
e.printStackTrace();
}
}
//the rest of the code
也发送这样的数据
Intent wall = new Intent(HomeActivityPro.this, GuessOne.class);
wall.putExtra("maxNumberPressed", conttext.getText().toString());
startActivity(wall);
这篇关于如何通过意图接收int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!