2012-01-02 8 views
2

私はアンドロイドを学び始めています。私はいくつかの質問があります。 私は私のメインプログラムを持っています、またアンドロイド - 入力データ(数値)で計算する - (グラフを表示する)

public class Radiation_overflowActivity extends Activity implements OnClickListener { 

    EditText init_cores; 
     View final_cores; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 


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

    //Set up click listeners 
    init_cores=(EditText) findViewById(R.id.init_cores); 
    //init_cores.setOnClickListener(this); 
    final_cores=(View) findViewById(R.id.final_cores); 
    final_cores.setOnClickListener(this); 

    } 

    //called when a button is clicked 
    public void onClick(View v) { 

     switch (v.getId()){ 

     case R.id.final_cores: 
      //int r = Integer.parseInt(init_cores.getText().toString().trim()); 
      double initcores=Double.parseDouble(init_cores.getText().toString().trim()); 
      double l=2,t=2; 
      double fcores=initcores*Math.exp(-l*t); 
      Intent i=new Intent(this,calcs.class); 
      i.putExtra("value",fcores); 
      startActivity(i); 
      break; 
     } 

     } 
     } 

私のmain.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/initial_cores" /> 
<EditText 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/init_cores" 
    android:inputType="numberDecimal" /> 
<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/final_cores" 
    android:text="@string/calculate" /> 
</LinearLayout> 

マイcalcs.java:

public class calcs extends Activity{ 

    TextView calcs_final; 

    public void onCreate(Bundle savedInstanceState) { 

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

     calcs_final=(TextView) findViewById(R.id.calcs_final); 

     double f=getIntent().getExtras().getDouble("value"); 

     calcs_final.setText(Double.toString(f)); 


} 

} 

マイcalcs.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/calcs_final" 
     android:text="@string/result" /> 


</LinearLayout> 

私はこれをやりたい:

ユーザはedittext(init_cores)にデータを入力してから、final_coresを計算してユーザに表示するための計算をいくつか行いたいと思います。

私は結果を表示するのに問題があります。別のアクティビティ(calcs.classとcalcs.xml)を開始しています。 ここで計算を行う必要がありますか?calcs.classに? 今すぐユーザーが数字を入力し、「計算」ボタンを押してから文字列(「string number」の文字列)を表示します。「結果数ではありません」

また、データから、私の計算から入力し、あなたが私にこれにいくつかの指示を与えることができれば?プロットを作る。

最後に、右?

ありがとう私のアプローチである!

答えて

0

私はあなたのfinal_coresが上の図で考える1として、あなたは計算のために、あなたのEditTextの値を取得したい、

だから、同じような何かを

public void OnClick(View v) { 

    switch (v.getId()){ 

    case R.id.final_cores: 
     int r = Integer.parseInt(init_cores.getText().toString().trim()); 
     Intent i=new Intent(this,calcs.class); 
     i.putExtra("value",r); 
     startActivity(i); 
     break; 
    } 

} 

削除(希望を私が間違っていないです) init_cores edittextのonClickListenerそして、これを試してみて、私が起こるか知ってみましょう...それが整数に解析可能なことができるよう

int r = getIntent().getExtras().getInt("value"); 

を使用してCalcs.classrの値はまた、あなたのエディットテキストに、常にあなたが数値を入力していることを確認し

を取得..

ありがとう、

+0

:助けてくれてありがとうございます。私は自分のコードを更新しました(私は二重の値を使用しました)。それをチェックしてください。今は、(正しい場所に計算していれば)結果が表示されます。 – George

+0

:二重値の場合は、 "double fcores = getIntent()。getExtras()。getDouble(" value ");" calcs.javaに?と私はcalcs.javaにこれを置く必要がありますか? – George

+0

はい、必要に応じてdouble値を使用します。double fcores = getIntent()。getExtras()。getDouble( "value"); calcs.javaにも同じアクティビティでも表示されます。 – user370305

1

あなたのデータをプロットするために非常に広範で使いやすいAChartEngineライブラリを使用することができます。

+0

非常に良いようです。ありがとう! – George

関連する問題