2012-09-27 87 views
2

我有一个简单的应用程序,它有一个edittext字段和一个按钮。该按钮我想获取edittext字段的值并将其存储到一个字符串中(稍后解析)。但是,即使使用getEditableText()方法读取值,我也会陷入困境。我打印出Log.d()的值,但该值看起来是空的或至少是一个空字符串。我哪里错了?Android 2.1 getEditableText()返回空/空字符串

activity_main.xml中

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

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="26dp" 
     android:text="@string/title" 
     tools:context=".MainActivity" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="38dp" 
     android:inputType="text" 
     android:ems="10" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/editText1" 
     android:layout_centerHorizontal="true" 
     android:text="@string/button1" 
     android:onClick="createarray" /> 

</RelativeLayout> 

MainActivity.java

package com.example.hw1; 
import android.os.Bundle; 
import android.app.Activity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

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

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    int[] numberarray; 

    public void createarray(View v){ 
     setContentView(R.layout.activity_main); 
     //EditText editText1 = (EditText)findViewById(R.id.editText1); 
     //Toast.makeText(this,editText1.getText().toString(), 
     //Toast.LENGTH_SHORT).show(); 
     //String etoutput = editText1.getText().toString(); 
     String etoutput = ((EditText)findViewById(R.id.editText1)).getEditableText().toString().trim(); 
     Log.d("etoutput",etoutput); 
     if(etoutput.length() > 0){ 
      Log.d("etoutput length","its got something"); 
     } 
     else{ 
      Log.d("etoutput length","EMPTY!!"); 
     } 
     //Log.d("etoutput","in here"); 
     //Log.d("etoutput",etoutput); 
     String[] sepetoutput = etoutput.split("[\\s,]+"); 

     numberarray = new int[sepetoutput.length]; 

     for (int i=0;i<sepetoutput.length;i++){ 
      try{ 
       Log.d("i",Integer.toString(i)); 
       numberarray[i] = Integer.parseInt(sepetoutput[i]); 
      }catch(NumberFormatException nfe){   

      } 
     } 

     Toast.makeText(this,"Array size: " + numberarray.length, 
       Toast.LENGTH_SHORT).show(); 
     ((EditText)findViewById(R.id.editText1)).setText("123456"); 

    } 
} 

回答

2

你每次调用setContentView()按下button。此操作会将EditText重置为您在Xml布局中定义的状态(该状态为空)。删除createarray方法的那一行,你应该得到你想要的。

+0

哦!感谢这工作! – gqdabien

+0

@gqdabien great和np – Aprian

0

你试过:

String etoutput = (EditText)findViewById(R.id.editText1)).getText().toString().trim(); 
+0

是的,我有getText()在第一次,但尝试getEditTableText()后阅读了类似的职位。仍然没有运气,得到0长度字符串回来。 – gqdabien

+0

实际上是否存在EditText中的文字? –

0

我想你应该在OnCreate过程中调用createarray函数。

+0

nope,不需要调用,因为'createarray'基本上是一个'onClick'方法,当点击'onClick ='createarray''属性的'button'时,会触发'onClick'方法。 – Aprian