在第一次单击editText时,不会传递这些值

时间:2015-11-02 11:45:18

标签: java android android-edittext onclicklistener

我需要通过editText从用户那里获取a和b的值,并将其传递给下一个activity.This是第一个活动:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.content.Intent;
import android.widget.EditText;

public class activitysecond extends AppCompatActivity {

    private String[] arraySpinner;
    private String[] arraySpinner2;
    int a=0,b=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondactivity);
        final EditText editText1=(EditText) findViewById(R.id.editText1);
        final EditText editText2=(EditText) findViewById(R.id.editText2);
        final Bundle bundle = new Bundle();
        Button EnterButton=(Button) findViewById(R.id.Enterbutton);

        this.arraySpinner = new String[]{
                "None", "Lightly Active", "Moderately Active", "Very Active"
        };
                Spinner s = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter = new ArrayAdapter<>
                (this,
                        android.R.layout.simple_spinner_dropdown_item, arraySpinner
                );

        s.setAdapter(adapter);

        this.arraySpinner2 = new String[]{
                "Male", "Female"
        };

                Spinner s1 = (Spinner) findViewById(R.id.spinner2);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<>
                (this,
                        android.R.layout.simple_spinner_dropdown_item, arraySpinner2
                );
        s1.setAdapter(adapter1);

        editText1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a = Integer.valueOf(editText1.getText().toString());
                bundle.putInt("one",a);
            }

        });

        editText2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                b = Integer.valueOf(editText2.getText().toString());
                bundle.putInt("two",b);
            }

        });

        EnterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent z = new Intent(activitysecond.this, activitythird.class);
                startActivity(z);
                z.putExtras(bundle);
            }
        });
    }
}

我要传递值的下一个活动是

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class activitythird extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.thirdactivity);

}

public void thirdbutton(View view){
    final TextView thirdtext=(TextView) findViewById(R.id.thirdtext);

    Bundle seconddata=getIntent().getExtras();
    int vala=seconddata.getInt("one");
    int valb=seconddata.getInt("two");

    thirdtext.setText(vala + " " +valb);


}

}

未传递值a和b,并且第三个文本不会更改 请帮忙!!

5 个答案:

答案 0 :(得分:1)

在将捆绑包添加到意图之前,您可以开始活动。

试试这样:

Intent z = new Intent(activitysecond.this, activitythird.class);
z.putExtras(bundle);
startActivity(z);

答案 1 :(得分:1)

删除编辑文本的ClickListener,并在EnterButton的onClick中添加以下行

  a = Integer.valueOf(editText1.getText().toString());
  bundle.putInt("one",a);
  b = Integer.valueOf(editText2.getText().toString());
  bundle.putInt("two",b);

答案 2 :(得分:1)

替换此

EnterButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent z = new Intent(activitysecond.this, activitythird.class);
        startActivity(z);
        z.putExtras(bundle);
    }
});

EnterButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent z = new Intent(activitysecond.this, activitythird.class);
        z.putExtras("one", Integer.parseInt(editText1.getText().toString()));
        z.putExtras("two", Integer.parseInt(editText2.getText().toString()));
        startActivity(z);
    }
});

答案 3 :(得分:1)

在下面; putExtra出现在startActivity之前,这就是错误。

EnterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent z = new Intent(activitysecond.this, activitythird.class);
            startActivity(z);
            z.putExtras(bundle);
        }
    });

尝试以下代码;

EnterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent z = new Intent(activitysecond.this, activitythird.class);

            z.putExtras(bundle); // first
            startActivity(z); //second
        }
    });

同样不能为您的案例实现editText onClickListeners。你应该通过以下方式获得变量:

if (editText1.getText() != null) { // to avoid exception
    a = Integer.valueOf(editText1.getText().toString());
    bundle.putInt("one",a);
}
if (editText2.getText() != null) { to avoid exception
    b = Integer.valueOf(editText2.getText().toString());
    bundle.putInt("two",b);
}

答案 4 :(得分:0)

修改你的代码如下:

&#xA;&#xA;
  EnterButton.setOnClickListener(new View.OnClickListener(){&#xA; @Override&#xA; public void onClick (View v){&#xA; Intent z = new Intent(activitysecond.this,activitythird.class);&#xA; z.putExtras(bundle);&#xA; startActivity(z);&#xA;}& #xA;});&#xA;  
&#xA;&#xA;

z.putExtras(bundle); 必须高于 startActivity(Z);

&#XA;