シンプルなコンバーターアプリを作りたいです。これが私が作りたいもののイメージです。実際には3つのレイアウト(アクティビティ)がありますが、実際にはこれら2つが最も重要です。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);
}
}
では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
「tempthing」と呼ばれる余分なものはありませんがdouble
、そのキーには余分なものがあります。
したがって、どちらかにint
値を入力しSecondActivity
ます:
myintent.putExtra("tempthing", (int)temp);
またはでdouble
値を取得しますThirdActivity
:
double doubleValue = myintent.getDoubleExtra("tempthing", 0);
それらは一致する必要があります。あなたが置く場合double
-読んでくださいdouble
。あなたが置く場合int
-読んでくださいint
:)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加