首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android2.1 getEditableText()返回null /空字符串

Android2.1 getEditableText()返回null /空字符串
EN

Stack Overflow用户
提问于 2012-09-27 10:59:31
回答 3查看 895关注 0票数 2

我有一个简单的应用程序,它有一个edittext字段和一个按钮。我想要获取edittext字段的值并将其存储到一个字符串中的按钮(稍后解析)。但是我甚至无法使用getEditableText()方法来读取值。我将值输出到Log.d(),但是值似乎是空的,或者至少是一个空字符串。我哪里错了?

activity_main.xml

代码语言:javascript
复制
<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

代码语言:javascript
复制
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");

    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-09-27 11:09:13

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

票数 2
EN

Stack Overflow用户

发布于 2012-09-27 11:04:08

您是否尝试过:

代码语言:javascript
复制
String etoutput = (EditText)findViewById(R.id.editText1)).getText().toString().trim();
票数 0
EN

Stack Overflow用户

发布于 2012-09-27 11:11:19

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12613445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档