【问题标题】:Android 2.1 getEditableText() returns null / empty stringAndroid 2.1 getEditableText() 返回 null / 空字符串
【发布时间】:2012-09-18 18:29:37
【问题描述】:

我有一个简单的应用程序,它有一个编辑文本字段和一个按钮。我想获取edittext字段的值并将其存储到字符串中的按钮(稍后解析)。但我什至坚持使用 getEditableText() 方法读取值。我正在将值打印到 Log.d() 但该值似乎为空或至少为空字符串。我哪里错了?

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");

    }
}

【问题讨论】:

    标签: android string methods gettext


    【解决方案1】:

    你试过了吗:

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

    【讨论】:

    • 是的,起初我在那里有 getText(),但在阅读了类似的帖子后尝试了 getEditTableText()。仍然没有运气,得到 0 长度的字符串。
    • EditText 中真的有文字吗?
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 不,不需要调用,因为createarray 基本上是一个onClick 方法,当带有onClick="createarray" 属性的button 被点击时触发。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      • 2020-09-28
      • 2017-03-05
      相关资源
      最近更新 更多