如何将int值从一个活动传递到另一个活动的文本视图?

时间:2014-07-28 14:59:25

标签: android android-layout android-activity

我想制作简单的转换器应用。 这是我想要创造的形象。实际上有三个布局(活动) 但实际上这两个问题最重要。可能有不需要的代码行cus不熟悉android开发。我努力做到这一点。我想现在我正在转换回到int的答案,但我仍然不知道如何在其他视图的Textview上显示它。

这是图像

https://drive.google.com/file/d/0B3siMB02QAyJLW1Ec3lkbDBWUVE/edit?usp=sharing

这是我的代码。感谢任何帮助!

public class SecondActivity extends ActionBarActivity {
EditText text = null;
int enterValue = 0;



public void convert(View view) {

    RadioButton celsiusButton = (RadioButton) findViewById(R.id.farenhite);
    RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.celcius);

    text = (EditText) findViewById(R.id.editText1);
    String temporary = text.getText().toString();
    int check = Integer.parseInt(temporary);

    if (check == 0) {
        Toast.makeText(this, "Not a valid number", Toast.LENGTH_LONG)
                .show();
        return;
    }

    float inputValue = check;

    if (celsiusButton.isChecked()) {
        // celFinal = inputValue;

        convertCelsiusToFahrenheit(inputValue);
        Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
        startActivity(intent);
    }
    if (fahrenheitButton.isChecked()) {

        convertFahrenheitToCelsius(inputValue);
        Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
        startActivity(intent);
    }

    if (celsiusButton.isChecked() && fahrenheitButton.isChecked()) {


        Toast.makeText(this, "You have selected both!", Toast.LENGTH_LONG)
                .show();

    } 

    /*
     * make buttons true and false if (fahrenheitButton.isChecked()) {
     * fahrenheitButton.setChecked(false); celsiusButton.setChecked(true); }
     * else { fahrenheitButton.setChecked(true);
     * celsiusButton.setChecked(false); }
     */

}

// Converts to celsius
private double convertFahrenheitToCelsius(double fahrenheit) {


    double temp = ((fahrenheit - 32) * 5 / 9);

    Intent myintent = new Intent(SecondActivity.this, ThirdActivity.class);
    myintent.putExtra("tempthing", temp);
    startActivity(myintent);
    return temp;

}

// Converts to fahrenheit
private double convertCelsiusToFahrenheit(double celsius) {


    double temp = ((celsius * 9) / 5) + 32;
    Intent myintent = new Intent(SecondActivity.this, ThirdActivity.class);
    myintent.putExtra("tempthing", temp);
    startActivity(myintent);
    return temp;
}

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



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.second, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

这是下一个视图的代码

公共类ThirdActivity扩展了ActionBarActivity {

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


    Intent intent = getIntent();
    if((intent.getExtras()!=null) && !intent.getExtras().isEmpty()){

        Intent myintent = getIntent();
        int intValue = myintent.getIntExtra("tempthing", 0);

        final TextView textViewTEST=(TextView) findViewById(R.id.textView1);
        textViewTEST.setText(intValue);

    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.third, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

3 个答案:

答案 0 :(得分:0)

示例:

Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
intent.putExtra("INTEGER",value);   
startActivity(intent);

答案 1 :(得分:0)

您可以使用以下代码

Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
intent.putExtra("your_key",value);   // your_key => tempthing
startActivity(intent);

并将值设置为ThirdActivity

final TextView textViewTEST=(TextView) findViewById(R.id.textView1);
 textViewTEST.setText(String.valueOf(intValue));

答案 2 :(得分:0)

原因:

SecondActivity中,您添加了double额外的" tempthing "使用putExtra(String key, Double value)方法使用附加内容。如果您将鼠标悬停在putExtra方法上,您会看到它使用带有double参数的变体。

double temp = ((fahrenheit - 32) * 5 / 9);
[...]
myintent.putExtra("tempthing", temp); <- puts double

但是在您的ThirdActivity中,您需要int额外使用getIntExtra方法。

int intValue = myintent.getIntExtra("tempthing", 0);

没有int额外被称为&#34; tempthing &#34;在内部 - 但是有一个double额外的那个键。

解决方案:

因此,您要将int值放在SecondActivity

myintent.putExtra("tempthing", (int)temp);

double中的ThirdActivity值:

double doubleValue = myintent.getDoubleExtra("tempthing", 0);

他们只需要匹配。如果你放double - 请阅读double。如果你放int - 阅读int:)