2013-02-06 29 views
0

我是新来的java和android所以请原谅我,如果我问简单的问题。 我有一个应用程序需要两个EditText中的用户输入。这些输入相乘并且结果显示在TextView中。我想使用“清除条目”按钮来清除用户条目的内容并显示结果。有什么办法可以做到吗?如何清除几个EditText中的条目?

这里是一个应用程序代码。

package c.example.rectangle; 

import android.os.Bundle; 

import android.view.View; 
import android.app.Activity; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 



public class MainActivity extends Activity implements OnClickListener{ 

    EditText l; 
    EditText w; 
    TextView a; 
    Button b; 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     l = (EditText) findViewById(R.id.length); 
     w = (EditText) findViewById(R.id.width); 
     a = (TextView) findViewById(R.id.lblarea); 
     b = (Button) findViewById(R.id.calculate); 

     b.setOnClickListener(this); 

    } 

    public void onClick(View v) { 
     calculateRectangle(l.getText().toString(), w.getText().toString()); 


    } 
    private void calculateRectangle(String clength, String cwidth){ 


     double area = Double.parseDouble(clength)*Double.parseDouble(cwidth); 

     a.setText(String.valueOf(area)); 
}} 

这是我的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:background="#8B4513" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/label1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="20dp" 
     android:background="#2F4F4F" 
     android:gravity="center" 
     android:text="@string/rect" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#8B4513" 
     android:orientation="horizontal" > 

     <TextView 
      android:id="@+id/label2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="30dp" 
      android:layout_marginTop="50dp" 
      android:background="#2F4F4F" 
      android:gravity="center" 
      android:text="@string/cm" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <EditText 
      android:id="@+id/length" 
      android:layout_width="110dp" 
      android:layout_height="21dp" 
      android:layout_marginLeft="40dp" 
      android:layout_marginTop="50dp" 
      android:background="#2F4F4F" 
      android:ems="10" 
      android:gravity="center" 
      android:hint="@string/help" 
      android:inputType="text" /> 


    </LinearLayout> 

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#8B4513" 
     android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/label3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#2F4F4F" 
     android:layout_marginLeft="30dp" 
     android:layout_marginTop="20dp" 
     android:text="@string/breadth" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <EditText 
     android:id="@+id/width" 
     android:layout_width="110dp" 
     android:layout_height="21dp" 
     android:layout_marginLeft="33dp" 
     android:layout_marginTop="20dp" 
     android:background="#2F4F4F" 
     android:inputType="text" 
     android:hint="@string/help" 

     android:ems="10" 
     android:gravity="center" 
     > 

     <requestFocus /> 
    </EditText> 

</LinearLayout> 

<Button 
    android:id="@+id/calculate" 
    android:layout_width="fill_parent" 
    android:layout_marginLeft="100dip" 
    android:layout_marginRight="100dip" 
    android:layout_height="wrap_content" 
    android:text="@string/calculate" 
    android:layout_marginTop="20dp" /> 

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#8B4513" 
     android:orientation="horizontal" > 

<TextView 
    android:id="@+id/label4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="30dp" 
    android:layout_marginTop="20dp" 
    android:background="#2F4F4F" 
    android:text="@string/area" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/lblarea" 
    android:layout_width="110dp" 
    android:layout_height="21dp" 
    android:layout_marginLeft="60dp" 
    android:layout_marginTop="20dp" 
    android:background="#2F4F4F" 
    android:gravity="center"/> 
</LinearLayout> 

<Button 
    android:id="@+id/clear" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="100dip" 
    android:layout_marginRight="100dip" 
    android:layout_marginTop="20dp" 
    android:text="@string/clear" /> 

</LinearLayout> 

我将是答案很欣赏。

+0

这里有很多很棒的答案!考虑标记其中一个被视为关闭这个问题:) –

回答

0

为什么不只需要将两个editTexts设置为空时,当你不再需要显示它们中的数据?

EditText.setText(""); 

与TextView相同的东西;

TextView.setText(""); 
+0

我同意。这就是我正要写的东西。 – edwoollard

0

如果你想创建一个清除按钮,做到以下几点。

在XML创建一个按钮:

<Button 
    android:id="@+id/clear_button" 
    ... you own layout prefs ... 
/> 

代码中的按钮创建一个监听:

OnClickListener clearButtonListener = new OnClickListener(){ 
    @Override 
    public void onClick(View view) {   
    ((EditText)findViewById(R.id.id_for_text_box_a)).setText(""); 
    //...do this for all your edit texts that you want to clear   
    } 
}; 

监听器连接到按钮

Button clearButton = (Button) findViewById(R.id.clear_button); 
clearButton.setOnClickListener(clearButtonListener); 

另外,而不是通过侦听器中的id找到编辑文本,它们可能是获取initiali的实例变量在onCreate或任何地方。我也建议不要使用一个字母的变量名称。

就个人而言,我会成立OnClickListener!而非在XML中onClick属性。虽然使用XML onClick属性可能会导致更少的代码行,但不幸的是,它会创建布局和功能的非常紧密的耦合。我更喜欢用XML来布局,用Java来实现功能。此外,被迫使用id等于需要XML元素具有变量名称,这使得更易读的布局代码(这个按钮是什么?这个复选框是什么?)。我在使用onClick属性时看到的另一个问题是,它迫使您调用的方法是公开的,这对于这些方法中的很多方法并不合适。 我更喜欢通过阅读Java来理解Activity的功能,并且我宁愿没有在我的Activities中浮动的未引用的公有方法。

+0

哇。这太棒了。谢谢 – Peter

+0

太棒了!让我更新,如果它适合你 –

0

如果你是想通过他们迭代,你可以把它们放在一个列表,然后使用一个for循环来将文本设置为“”

List<EditText> myList = new List<EditText>(); 
myList.add(editText1); 

然后在您的明确方法

for (int x = 0; x < myList.size(); x++ 

{ 
myList.get(x).setText(""); 
} 
+0

谢谢大家。我没有尝试codeMagic answear,但它也看起来很好。再次感谢你。 – Peter

+0

不客气,很高兴你解决了。他们都会做这项工作。当我第一次看到你的问题时,我以为你想通过迭代它们来一次完成它们。如果你接受你使用过的答案,它会帮助其他人发现同样的问题更容易:) – codeMagic

0

为什么没有人似乎使用真正有用的android:onClick

<Button 
    ... 
    android:text="@string/calculate" 
    android:onClick="calculate" /> 
<Button 
    ... 
    android:text="@string/clear" 
    android:onClick="clearForm" /> 

与下列活动:

class MyActivity extends Activity 
{ 
    ... 
    /** 
    * Calculate 
    * android:onClick="calculate" 
    */ 
    public void calculate(View view) 
    { 
     // Handle click on your 'Calculate' button 
    } 

    /** 
    * Clear form 
    * android:onClick="clearForm" 
    */ 
    public void clearForm(View view) 
    { 
     int[] ids = new int[]{R.id.length, R.id.width}; 
     for(int id : ids) 
     { 
      ((EditText) this.findViewById(id).)setText(""); 
     } 
    } 
} 

这样,您就不必在意ID和你的代码会比干净更干净。

不应该滥用ID!他们擅长处理这些变化(并且享受onSaveInstanceState()的自然行为),可以由用户“改变”,但就是这样!

IMO。

+0

我更喜欢布局和功能之间的松散耦合。看到我更新的答案为我的两美分:) –

+0

耦合?在某种方式。 我从来不会为一个活动使用多个布局,但有时我会为一个以上的活动使用一个布局......然后实现这些android:onClick方法。 确实,我同意,这有一个耦合,但对于Android应用程序设计和清晰而干净的源代码来说这是一个糟糕的结果吗? ;) – Gorcyn