将edittext的值存入变量并将其打印到LogCat中

时间:2013-10-04 12:37:42

标签: java android android-edittext gettext

我是Android Development和Java的新手。我有问题将EditText的值存储到变量中,然后在控制台中将其打印出来。 我已经阅读了关于getText(),但我不太明白它是如何工作的。 这是我的剧本:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/simplenote_mainpage_background_200"
android:paddingBottom="0px"
android:paddingLeft="0px"
android:paddingRight="0px"
android:paddingTop="0px"
tools:context=".MainActivity" >

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</RelativeLayout>

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="41dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="2dp"
    android:layout_marginRight="1dp"
    android:text="Save"
    android:textColor="#FFFFFF"
    android:textSize="20sp"
    android:typeface="sans" />

<EditText
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/relativeLayout1"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="10dp"
    android:textStyle="bold"
    android:hint="Title.."
    android:ems="10" />

<EditText
    android:id="@+id/note"
    android:layout_width="285dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/title"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="15dp"
    android:hint="Note.."
    android:ems="10" >

    <requestFocus />
</EditText>



</RelativeLayout>

    public class notescreen extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notescreen);

    EditText text = (EditText)findViewById(R.id.title);
    String value = text.getText().toString();



   // TODO Auto-generated method stub
}

}

我尝试过你建议的一些事情,因为我遇到了很多问题: 我想上传截图,但我没有10个声誉。 这是编辑过的代码:

package com.example.simplenote;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

public class notescreen extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notescreen);

    Button b1 = (Button)findViewById(R.id.button1);
    TextView tv = (TextView)findViewById(R.id.tv);
    EditText title = (EditText)findViewById(R.id.title);

     b1.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

             String value = title.getText().toString();

            tv =settext(value);

                }
            });
     final String TAG = "MyClass";
             Log.d(TAG, value);
    //EditText text = (EditText)findViewById(R.id.title);
    //String value = text.getText().toString();



   // TODO Auto-generated method stub
}


  }

3 个答案:

答案 0 :(得分:0)

public class notescreen extends Activity {


//Its a good practice to declare all the things that you want in activity
String value;
Button b1;
TextView tv;
EditText title;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notescreen);

// here you have to initialize by its id
b1 = (Button)findViewById(R.id.button1);
tv = (TextView)findViewById(R.id.tv);
title = (EditText)findViewById(R.id.title);

 b1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

         value = title.getText().toString();

        tv.settext(value);

            }
        });


  }

}

在xml文件中创建TextView

<TextView
android:id="@+id/tv"
android:layout_width="285dp"
android:layout_height="wrap_content"

 >

答案 1 :(得分:0)

从一开始,你不是存储值的变量。

EditText text = (EditText)findViewById(R.id.title);

上面一行,即存储对EditText的引用。这允许您调用与EditText相关的该对象上的函数,即getText()setText()。您还可以调用其他功能,但这两个功能是您要使用的主要功能。

getText从文本框中检索值,例如如果你输入文本框“hello”,那么getText()将返回字符串“hello”。 getText()将始终返回一个字符串,即使输入了一个数字。

setText设置文本框中显示的内容。例如。如果你在执行此行时运行s text.setText("hello");,它将在编辑文本中显示“hello”。

String value = text.getText().toString();

上面的行是您存储编辑文本的实际值的位置。因此,使用上面的示例,您在编辑文本中键入“hello”,当调用上面的行时,变量value现在将等于"hello"

要将此变量输出到控制台,有一个名为Log的类。这个类有各种功能输出到控制台(在android中也称为logcat)。

各种类型

Log.d =调试类型消息 Log.e =表示错误的消息(红色) Log.v =详细消息(绿色) Log.w =警告消息(黄色)

我认为还有其他一些但我不记得了。

要输出具有text.getText()函数返回值的变量,请将此变量传递给上述Log函数之一。

上述每个日志函数都有两个参数,tag参数和value参数。

tag参数只是一个名称,因此您可以过滤消息以仅显示该名称,为标记创建静态最终字符串是很常见的,例如在下面的行中。< / p>

private static final string TAG = "MyClass"

然后输出文本变量,您将使用以下行

Log.d(TAG, value);

如果您想让消息更清楚地表明该值代表什么,可以在开头连接一个字符串,以便可能成为

Log.d(TAG, "The value of my edit text is: " + value);

使用上面的示例,如果您在编辑文本中输入了hello,则日志猫将显示以下内容之一,具体取决于您在上面使用的行

MyClass       hello
MyClass       The value of my edit text is: hello

希望这有帮助

答案 2 :(得分:0)

试试这个:

//Add your packagename here please

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class notescreen extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        // Here is where you call the super on this method.
        // Mostly always comes as first line of any onCreate.
        super.onCreate(savedInstanceState);

        // After you have your layout set, you can retrieve your references
        setContentView(R.layout.notescreen);

        // You have just recovered your view class
        final EditText text = (EditText) findViewById(R.id.title);

        // You have just recovered the text of that view class (text)
        // final String value = text.getText().toString();

        // The log works as a printer on the console
        // Log.d("notescreen", "This is the text: " + value);

        // But hey... there is no text right "onCreate", right?
        // Soo...

        // Retrieve a button (you will need to create it on XML first)
        final Button clickForTextBt = (Button) findViewById(R.id.clickfortextbt);

        // Make that button print your message
        clickForTextBt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("notescreen", "This is the text: " + text.getText());
            }
        });
    }
}