我是新来的,我希望我早日得到回答。我创建了我的第一个Android应用程序,我想做一些特别的事情:如果用户在numberpicker中输入“1”,我想要一个按钮来显示活动,如果用户输入另一个数字,则显示另一个活动。 但是使用下一个代码,它始终显示第一个活动(名为“trip”)。有人可以帮帮我吗? 如果您还有其他需要回答,请告诉我。
public class numberoftrips extends AppCompatActivity {
private Button btnNext2;
private EditText nbtrip;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numberoftrips);
btnNext2 = (Button) findViewById(R.id.btnNext2);
nbtrip=(EditText)findViewById(R.id.nbtrip1);
final int n=Integer.parseInt(nbtrip.getText().toString());
btnNext2.setOnClickListener(new View.OnClickListener(){
public void onClick (View view) {
if (n==1){
Intent myIntent =new Intent(getBaseContext(), trip.class);
startActivityForResult(myIntent,0);
}
else{
Intent myIntent =new Intent(getBaseContext(), ID_informations.class);
startActivityForResult(myIntent,0);
}
}
});
}
}
PS:对不起我的英文,我是法国人。
答案 0 :(得分:0)
单击按钮后,在EditText中获取文本。
答案 1 :(得分:0)
实际上这是正常的。
您说的数字是n
,如果我是对的。
当您的活动启动并启动onCreate()
功能时,她会在nbtrip
的包含值处启动n但在活动开始时。
如果您希望获得之后的EditText 的值,您可以更改其文本,那么您有两种可能性
1:设置changeTextListener
public class numberoftrips extends AppCompatActivity {
private Button btnNext2;
private EditText nbtrip;
// add this here is necessary. If you declare it in a function
// and use it in a listener, it must be final, and you will not change it.
private int myActuallyValue = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numberoftrips);
btnNext2 = (Button) findViewById(R.id.btnNext2);
nbtrip = (EditText) findViewById(R.id.nbtrip1);
// final int n=Integer.parseInt(nbtrip.getText().toString());
nbtrip.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// when the text change this function is automaticaly called and
// you will get the charSequance, put in a String object
// and get the int of the character of the String object.
myActuallyValue = Integer.valueOf(String.valueOf(s));
}
@Override
public void afterTextChanged(Editable s) {
}
});
btnNext2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// when you click on the button, you get the saved value when
// you edited your EditText
if (myActuallyValue == 1) {
Intent myIntent = new Intent(getBaseContext(), trip.class);
startActivityForResult(myIntent, 0);
} else {
Intent myIntent = new Intent(getBaseContext(), ID_informations.class);
startActivityForResult(myIntent, 0);
}
}
});
}
}
2:点击按钮
时获取值public class numberoftrips extends AppCompatActivity {
private Button btnNext2;
private EditText nbtrip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numberoftrips);
btnNext2 = (Button) findViewById(R.id.btnNext2);
nbtrip = (EditText) findViewById(R.id.nbtrip1);
btnNext2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// here, you get the charSequance of the editText (after the onCreate finished)
// then you get the String object of the charSequence with .toString
// and you get the int value of the String object with Integer.valueOf()
if (Integer.valueOf(nbtrip.getText().toString()) == 1) {
Intent myIntent = new Intent(getBaseContext(), trip.class);
startActivityForResult(myIntent, 0);
} else {
Intent myIntent = new Intent(getBaseContext(), ID_informations.class);
startActivityForResult(myIntent, 0);
}
}
});
}
}