2014-04-24 276 views
3

我對Android開發還不熟悉,我想要做的是創建一個偵聽器,該偵聽器將包含兩個保存區域和邊界的TextView對象。寬度和高度是EditText對象。在輸入寬度和高度後,應根據calcArea和calcPerimeter方法實時顯示周長和麪積的值。我用於聽衆的代碼是基於我在這裏找到的一個例子。我的代碼:TextChanged監聽器

package com.jtryon.rectanglecalc; 

    import android.support.v7.app.ActionBarActivity; 
    import android.support.v7.app.ActionBar; 
    import android.support.v4.app.Fragment; 
    import android.text.Editable; 
    import android.text.TextWatcher; 
    import android.os.Bundle; 
    import android.view.LayoutInflater; 
    import android.view.Menu; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.EditText; 
    import android.widget.TextView; 
    import android.os.Build; 

    public class MainActivity extends ActionBarActivity { 

     // fields in the class 
     // variables that are global to this file 
     double width; 
     double height; 
     double area; 
     double perimeter; 

     // "handles" to the objects from the XML 
     EditText widthEdit; 
     EditText heightEdit; 
     TextView areaText; 
     TextView perimText; 

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

      // set up handles 
      widthEdit = (EditText)findViewById(R.id.width_edit); 
      heightEdit = (EditText)findViewById(R.id.height_edit); 
      areaText = (TextView)findViewById(R.id.area_value); 
      perimText = (TextView)findViewById(R.id.perim_value); 

      widthEdit.addTextChangedListener(
        new TextWatcher() { 

         @Override 
         public void afterTextChanged(Editable arg0) { 
          // TODO Auto-generated method stub 

          // read the width out of widthEdit 
          String widthString = widthEdit.getText().toString(); 

          // convert the String into a double 
          if (widthString.length() > 0) { 
           width = Double.parseDouble(widthString); 
          } 

          // read the height out of heightEdit 
          String heightString = heightEdit.getText().toString(); 

          if (heightString.length() > 0) { 
           height = Double.parseDouble(heightString); 
          } 


          // calculate area 
          double area = calcArea(); 

         // calculate perimeter 
         double perim = calcPerim(); 

         // set the label for areaText 
         areaText.setText(Double.toString(area)); 

         // set the label for perimText 
         perimText.setText(Double.toString(perim)); 

        } 

        @Override 
        public void beforeTextChanged(CharSequence s, int start, 
          int count, int after) { 
         // TODO Auto-generated method stub 

        } 

        @Override 
        public void onTextChanged(CharSequence s, int start, 
          int before, int count) { 
         // TODO Auto-generated method stub 

        } 
       } 
     ); 
    } 

    double calcArea() 
    { 
     return width * height; 
    } 

    double calcPerim() 
    { 
     return 2 * width * height; 
    } 
} 

User interface

+0

定義這個您的EditText下是什麼問題? – Libin

+0

我想發佈截圖,但我沒有足夠的聲望點。問題是它必須實時更新,我的猜測是我需要使用某種類型的偵聽器。一旦焦點離開了EditText對象,它將不得不更新,所以不太確定如何實現代碼。 – JimT

+0

我在原始文章中添加了一張圖片來展示我想要完成的任務。 – JimT

回答

1

中的onCreate

youredittext.addTextChangedListener(new TextWatcher() 
    { 
     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) 
     { 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int aft)                   
     { 

     } 

     @Override 
     public void afterTextChanged(Editable s) 
     { 

      //call your function here of calculation here 
      yourfunctioname(); 

     } 
    }); 
+0

這將做到 –

+0

快樂編碼... –

0

你能澄清你試圖用一個聽衆2個textviews完成什麼一點點。我的第一個建議是創建一個接受textview作爲參數的回調。 然後讓你的班級接受由這兩個班級創建的2個回調。不確定的問題。

+0

這不是一個答案,這應該是一個評論。 –

+0

Can not comment without 50 rep – Adam

0

添加OnFocusChangeListenerEditText的意見。

而當其不集中,得到的值,做你的計算和更新TextView

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if(!hasFocus) { 
       // add your code to process the values entered 
      } 
     } 
    }); 
+0

OnFocusChangeListener和addTextChangedListenener有什麼區別? – JimT

1

試試這個代碼 -

activity_main.xml中

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

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="width" /> 

    <EditText 
     android:id="@+id/edit_width" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="0" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="height" /> 

    <EditText 
     android:id="@+id/edit_height" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="0" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="area" /> 

    <TextView 
     android:id="@+id/edit_area" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="0" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="perimeter" /> 

    <TextView 
     android:id="@+id/edit_perimeter" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="0" /> 

</LinearLayout> 

MainActivity.java -

public class MainActivity extends ActionBarActivity { 

    EditText edit_width, edit_height; 
    TextView edit_area, edit_perimeter; 

    double width; 
    double height; 
    double area; 
    double perimeter; 

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

     edit_area = (TextView) findViewById(R.id.edit_area); 
     edit_height = (EditText) findViewById(R.id.edit_height); 
     edit_width = (EditText) findViewById(R.id.edit_width); 
     edit_perimeter = (TextView) findViewById(R.id.edit_perimeter); 

     edit_width.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 
       // TODO Auto-generated method stub 



      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       // TODO Auto-generated method stub 


       String widthString = edit_width.getText().toString(); 

       // convert the String into a double 
       if (widthString.length() > 0) { 
        width = Double.parseDouble(widthString); 
       } 

       // read the height out of heightEdit 
       String heightString = edit_height.getText().toString(); 

       if (heightString.length() > 0) { 
        height = Double.parseDouble(heightString); 
       } 

       // calculate area 
       double area = calcArea(); 

       // calculate perimeter 
       double perim = calcPerim(); 

       // set the label for areaText 
       edit_area.setText(Double.toString(area)); 

       // set the label for perimText 
       edit_perimeter.setText(Double.toString(perim)); 

      } 
     }); 

     edit_height.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       // TODO Auto-generated method stub 

       String widthString = edit_width.getText().toString(); 

       // convert the String into a double 
       if (widthString.length() > 0) { 
        width = Double.parseDouble(widthString); 
       } 

       // read the height out of heightEdit 
       String heightString = edit_height.getText().toString(); 

       if (heightString.length() > 0) { 
        height = Double.parseDouble(heightString); 
       } 

       // calculate area 
       double area = calcArea(); 

       // calculate perimeter 
       double perim = calcPerim(); 

       // set the label for areaText 
       edit_area.setText(Double.toString(area)); 

       // set the label for perimText 
       edit_perimeter.setText(Double.toString(perim)); 

      } 
     }); 

    } 

    double calcArea() { 
     return width * height; 
    } 

    double calcPerim() { 
     return 2 * width * height; 
    } 

} 

希望這可以幫助你!

如果它不工作,請讓我知道,我會盡力幫助你更..

+0

您爲區域和邊界EditText對象而不是TextView對象創建的原因是什麼?我只希望能夠編輯寬度和高度,然後基於此,周長和麪積應該更新。 – JimT

+0

好的,所以你可以使用textView而不是editText作爲區域和邊界。 –

+0

現在你可以檢查我編輯的答案。我將editText改爲textview。 –