从编辑文本获取文本到另一个活动

时间:2016-03-23 05:57:42

标签: java android android-studio

我有一项主要活动,结果和结果2 ..... 我想从活动结果,然后从结果1到结果2。 但我的代码似乎不起作用帮助!

这是主要活动////////

public class MainActivity extends SampleActivityBase {

// Whether the Log Fragment is currently shown
private boolean mLogShown;
public static final String TAG = "MainActivity";

EditText cardnumber;
TextView foodorders;
Button btnorder;
private static final int REQUEST_CODE = 10;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cardnumber = (EditText) findViewById(R.id.card_account_field);
    cardnumber.setText(AccountStorage.GetAccount(getBaseContext()));
    cardnumber.addTextChangedListener(new AccountUpdater());
    foodorders = (TextView) findViewById(R.id.foodordershow);
    btnorder = (Button) findViewById(R.id.buttonorder);


    btnorder.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String strcardnumber = cardnumber.getText().toString();
            Intent intent = new Intent(MainActivity.this, activityresult1.class);
            intent.putExtra("Card Number:", strcardnumber);
            startActivityForResult(intent, REQUEST_CODE);
        }
    });

}

private class AccountUpdater implements TextWatcher {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // Not implemented.
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // Not implemented.
    }

    @Override
    public void afterTextChanged(Editable s) {
        String account = s.toString();
        AccountStorage.SetAccount(getBaseContext(), account);
    }

这是结果1

  public class activityresult1 extends Activity {


EditText frq, fnq, foodordershow;
Button suborder;
TextView noodlequantity, ricequantity;
private static final int REQUEST_CODE = 10;

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

    fnq = (EditText) findViewById(R.id.fnq);
    frq = (EditText) findViewById(R.id.frq);
    noodlequantity = (TextView) findViewById(R.id.noodlequantity);
    ricequantity = (TextView) findViewById(R.id.ricequantity);
    suborder = (Button) findViewById(R.id.suborder);


    suborder.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String strnoodlequantity = noodlequantity.getText().toString();
            String strricequantity = ricequantity.getText().toString();
            Intent intent = new Intent(getApplicationContext(), activityresult2.class);
            intent.putExtra("Noodle quantity", strnoodlequantity);
            intent.putExtra("Rice quantity", strricequantity);
            startActivityForResult(intent, REQUEST_CODE);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
        if (data.hasExtra("Food Ordered:")) {
            String result = data.getExtras().getString("Food Ordered:");
            if (result != null && result.length() > 0) {
                foodordershow.setText("Food Ordered: " + result);
            }
        }
    }
}

最后结果2

 public class activityresult2 extends Activity {

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

    Bundle extras = getIntent().getExtras();
    String strfnq = extras.getString("Noodle Quantity");
    String strfrq = extras.getString("Rice Quantity");
    TextView foodorders = (TextView) findViewById(R.id.foodordershow);
    foodorders.setText(getIntent().getExtras().getString("Quantity"));

    foodorders = (TextView) findViewById(R.id.foodordershow);
    foodorders.setText("Welcome, " + strfnq + " + strfrq + ");
    finish();


}

1 个答案:

答案 0 :(得分:0)

我想也许您可以尝试使用Bundle打包数据

mainactivity

public class MainActivity extends SampleActivityBase {

// Whether the Log Fragment is currently shown
private boolean mLogShown;
public static final String TAG = "MainActivity";

EditText cardnumber;
TextView foodorders;
Button btnorder;
private static final int REQUEST_CODE = 10;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cardnumber = (EditText) findViewById(R.id.card_account_field);
    cardnumber.setText(AccountStorage.GetAccount(getBaseContext()));
    cardnumber.addTextChangedListener(new AccountUpdater());
    foodorders = (TextView) findViewById(R.id.foodordershow);
    btnorder = (Button) findViewById(R.id.buttonorder);


    btnorder.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String strcardnumber = cardnumber.getText().toString();
            Intent intent = new Intent(MainActivity.this, activityresult1.class);
            intent.putExtra("Card Number:", strcardnumber);
            //if you need to let activityresult1 back to MainActivity with data, you can use startActivityForResult, if not just use startActivity(intent).
            startActivityForResult(intent, REQUEST_CODE);
        }
    });

}

private class AccountUpdater implements TextWatcher {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // Not implemented.
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // Not implemented.
    }

    @Override
    public void afterTextChanged(Editable s) {
        String account = s.toString();
        AccountStorage.SetAccount(getBaseContext(), account);
    }

这是结果1

public class activityresult1 extends Activity {


EditText frq, fnq, foodordershow;
Button suborder;
TextView noodlequantity, ricequantity;
private static final int REQUEST_CODE = 10;

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

    fnq = (EditText) findViewById(R.id.fnq);
    frq = (EditText) findViewById(R.id.frq);
    noodlequantity = (TextView) findViewById(R.id.noodlequantity);
    ricequantity = (TextView) findViewById(R.id.ricequantity);
    suborder = (Button) findViewById(R.id.suborder);


    suborder.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String strnoodlequantity = noodlequantity.getText().toString();
            String strricequantity = ricequantity.getText().toString();
            Intent intent = new Intent(getApplicationContext(), activityresult2.class);
            Bundle bundle = new Bundle();
            bundle.putString("Noodle quantity", strnoodlequantity);
            bundle.putString("Rice quantity", strricequantity);
            //intent.putExtra("Noodle quantity", strnoodlequantity);
            //intent.putExtra("Rice quantity", strricequantity);
            intent.putExtras(bundle);
            startActivityForResult(intent, REQUEST_CODE);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
        if (data.hasExtra("Food Ordered:")) {
            String result = data.getExtras().getString("Food Ordered:");
            if (result != null && result.length() > 0) {
                foodordershow.setText("Food Ordered: " + result);
            }
        }
    }
}

最后结果2

public class activityresult2 extends Activity {

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

    Bundle extras = getIntent().getExtras();
    String strfnq = extras.getString("Noodle Quantity");
    String strfrq = extras.getString("Rice Quantity");
    TextView foodorders = (TextView) findViewById(R.id.foodordershow);
    foodorders.setText(getIntent().getExtras().getString("Quantity"));

    foodorders = (TextView) findViewById(R.id.foodordershow);
    foodorders.setText("Welcome, " + strfnq + " + strfrq + ");
    //finish();


}