2011-10-30 4 views
0

XMLで既に配置された4つの編集テキストボックスがあり、他の2つの行はプラスボタンをクリックして追加できます。その下にxmlとjavaコードの値を持つボタンイベント

ここでは、これらの編集テキストボックスに格納されている値に関連するイベントを割り当てるボタン名を計算しています。

したがって、これらの両方で

<edit text1> <edit text2> 
< edit text3> <edit text4> 
<edit text 5> <edit text6> 

のように私はいくつかの値を入力すると、私は

((edit text1)*(edit text2))+((edittext3)*(edit text4))+((edittext5)*(edit text6)) 

をしたいと私は計算をクリックしたときに、このイベントが行われるべきであると言うことができますされます。私はそれをやったことがわかるように、ボタンのクリックイベントを行う方法を知っています。これらの値を解析して電卓のクリックイベントに役立てる必要があります。値はXMLからテキストボックスを編集したり、プラスボタンによって生成される値を編集したりします。

また、レイアウトを少し変更したいと思います。私はそれを使って遊んだりしようとしましたが、今は編集テキストボックスの下にボタンを置くことができましたが、プラスボタン+クリックボックスをクリックすると下に来ます。上記の編集テキストのレイアウトと同じです。

Javaコード:

import android.app.Activity; 

import android.os.Bundle; 
import android.view.Gravity; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

    public class PlusbuttonActivity extends Activity 
    implements OnClickListener { 

     private TextView tt; 
     private TextView j; 
     /** Called when the activity is first created. */ 
     private LinearLayout root; 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

      View btnButton = findViewById(R.id.button_next); 
      Button mButton = (Button) findViewById(R.id.button); 
      mButton.setGravity(Gravity.CENTER); 

      tt=(TextView)findViewById(R.id.tt); 
      j=(TextView)findViewById(R.id.j); 

      root = (LinearLayout) findViewById(R.id.linearLayout); 
      mButton.setOnClickListener(this); 
     } 

      @Override 
      public void onClick(View v) { 
       switch (v.getId()) { 
       case R.id.button: 
       View view = doStuff(); 
        addViewToRoot(view); 
       break; 
       case R.id.button_next: 
        View view1 = doCalc(); 
        addViewToRoot(view1); 
        break; 

       } 
      } 

      private View doCalc() { 
       // but these are text boxes.. i want to implement the edit boxes and output the result in text box 
       int parsedInt = Integer.parseInt(tt.getText().toString()); 
       int parsedIn = Integer.parseInt(j.getText().toString()); 




       return null; 
      } 

     private View doStuff() { 
       EditText t = new EditText(PlusbuttonActivity.this); 
       t.setGravity(Gravity.LEFT); 
       t.setWidth(250); 
       EditText a = new EditText(PlusbuttonActivity.this); 
       a.setGravity(Gravity.RIGHT); 
       a.setWidth(250); 
       LinearLayout l = new LinearLayout(PlusbuttonActivity.this); 

       t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
       // t.setBackgroundColor(0xffCECECE); 


       a.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

       l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

       l.addView(t); 
       l.addView(a); 

       return l; 


     }   

     private void addViewToRoot(View v){ 
      root.addView(v); 
     } 

    } 

XMLファイル:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:id="@+id/linearLayout"> 


    <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="left" 
       android:layout_weight="1" 
       android:text="Units" 
       android:id="@+id/tt" 
       /> 
      <TextView android:layout_height="wrap_content" 
      android:gravity="right" 
      android:layout_width="wrap_content" 
      android:layout_weight="1" 
      android:text="Grades" 
      android:id="@+id/j"></TextView> 
     </LinearLayout> 
    <LinearLayout android:id="@+id/LinearLayout01" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:orientation="horizontal"> 

       <EditText 
       android:layout_width="250px"  
       android:layout_height="wrap_content"  
       android:id="@+id/edLat"> 
       </EditText> 

       <EditText  
       android:layout_height="wrap_content"  
       android:id="@+id/edLong" android:layout_width="150dp"> 
       </EditText> 
     </LinearLayout> 

    <LinearLayout android:id="@+id/LinearLayout01" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:orientation="horizontal"> 

       <EditText 
       android:layout_width="250px"  
       android:layout_height="wrap_content"  
       android:id="@+id/et2"> 
       </EditText> 

       <EditText  
       android:layout_height="wrap_content"  
       android:id="@+id/et1" android:layout_width="150dp"> 
       </EditText> 
     </LinearLayout> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

     <Button android:layout_height="wrap_content" 
     android:gravity="center" android:id="@+id/button" android:text="+" android:layout_width="wrap_content"></Button> 

    <Button 
    android:id="@+id/button_next" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_alignParentRight="true" 
    android:text="CALCULATE" 
    > 
    </Button> 
    </RelativeLayout> 



    </LinearLayout> 
+0

JJ、トンを作ってください、ルート秘密 – hovanessyan

+0

は、私はまた、あなたの活動は、OnClickListenerインターフェースを実装する必要があることを、示唆するかもしれない - あなたのコードは、この方法できれいになります。 – hovanessyan

+0

umm ...これをキャッチできないコードを編集できますか? – Gagandeep

答えて

0

現在のコードがわかりません。このコードセクションで

  1. @Override 
        public void onClick(View v) { 
         switch (v.getId()) { 
         case R.id.button: 
         View view = doStuff(); 
          addViewToRoot(view); 
         break; 
         case R.id.button_next: 
          View view1 = doCalc(); 
          addViewToRoot(view1); 
          break; 
         } 
        } 
    

は、なぜあなたは "button_next" のaddViewToRootを呼び出していますか? 結果を別のアクティビティで表示しないようにしますか? あなたが使用する必要があります。

    case R.id.button_next: 
       startActivity(new Intent(this, YourShowCalcResultsActivity.class)); 
       break; 
  1. 私はあなたが一番上にあるもののレイアウト要素を挿入達成するためにRelativeLayoutを使用することを検討すべきだと思います。要するに、doStuff()で作成したLinearLayoutをRelativeLayoutにラップします。親レイアウトをRelativeLayoutにする必要もあります。 addRules()を使用して、好きなように要素を揃えることができます。

  2. 3.
+0

私は同じクラスでそれらを実装したいです。 :$ – Gagandeep

+0

あなたは同じアクティビティのすべてを欲しいですか?あなたは計算をどこに表示しますか? – hovanessyan

+0

同じウィンドウで... – Gagandeep

0

私が正しく理解していれば、私は見てみましょう。それぞれのEditTextから数値を解析して、算術演算を実行できるようにしたいとします。

まず、EditTextへの参照を取得します。 EditTextがコードに行われた場合のEditTextがXMLで行われた場合、あなたはそれにIDを割り当ててくださいと

EditText et = (EditText)findViewById(R.id.editID); 

を使用し、あなたはすでにそれへの参照を持つべきで、そうfindViewByIdを使用する必要はありません。あなたが解析したら

Float.parseFloat(et.getText().toString()); 

:floatを解析するに

Double.parseDouble(et.getText().toString()); 

:のEditTextからダブルを解析するに

int parsedInt = Integer.parseInt(et.getText().toString()); 

:のEditTextからint型を解析するために

あなたが望む任意のタイプを使用する数字は、算術演算は簡単です。

+0

しかし、これは私はXMLの編集テキストから行うことができます...しかし、+ボタンで生成されているものについては、Javaでクリック? – Gagandeep

+0

これは、XMLを使っているのか、コードを使っているのかにかかわらず、あなたがそれを参照している限り、どんなEditTextでも行うことができます。 –

+0

//変更された \t amount1 =(EditText)findViewById(R.id.et1); (EditText)findViewById(R.id.et2);\t \t \t \tボタンbutton_next =(ボタン)findViewById(R.id.button_next); \t button_next.setOnClickListener(新Button.OnClickListener() \t {ます。public void onClickの \t(ビューV){(計算);} \t \tプライベートボイド計算(){ \t \t \t // TODO自動生成方法スタブ \t \t \t Double.parseDouble(amount1.getText()のtoString()); \t \t \t Double.parseDouble(amount2.getText()のtoString()); \t \t}}); \t} \tが... – Gagandeep

関連する問題