Android [错误]:一个按钮应显示2个活动

Android [错误]:一个按钮应显示2个活动,android,android-edittext,Android,Android Edittext,我是新来的,希望能早日得到答复。我制作了我的第一个Android应用程序,我想做一些特别的事情:如果用户在numberpicker中输入“1”,我需要一个按钮来显示一个活动,如果用户输入其他数字,我需要一个按钮来显示另一个活动。 但是对于下一个代码,它总是显示第一个活动(名为“trip”)。谁能帮帮我吗? 如果你还需要什么回答,请告诉我 public class numberoftrips extends AppCompatActivity { private Button btnNext2;

我是新来的,希望能早日得到答复。我制作了我的第一个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:对不起我的英语,我是法国人。

点击按钮后将文本放入EditText。

实际上这是正常的

如果我是对的,你说的号码是
n

当您的活动开始并启动
onCreate()
函数时,她在
nbtrip
的contain值处启动n,但在活动开始时启动。 如果您想在更改EditText后获得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);
                }
            }
        });   
    }
}

您的行
final int n=…
将其从原来的位置移除,并将其放在onclick方法中。非常感谢。如果我在其他活动中需要数字“n”怎么办?