无法从editText Android Eclipse获取文本

时间:2014-03-15 22:17:25

标签: java android eclipse android-layout import

我在学校项目中遇到了一些问题。我无法从文本框中获取文本。我搜索了一个解决方案但没有找到任何结果......如果有人帮助我,我将感激不尽:)

所以这是我的Java代码:

package com.src.vicnote;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;

public class NewNoteActivity extends ActionBarActivity {

    Button saveButton;
    EditText textData;
    Context context;

    String text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_note);

        saveButton = (Button) this.findViewById(R.id.buttonSave);
        saveButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                textData = (EditText) findViewById(R.id.editText);
                text = textData.getText().toString();
                //text = "this is sparta!";
                Log.d("ADebugTag", "string: \"" + text + "\" end of note text");
                new SaveClass(text/*, context.getApplicationContext()*/);
            }
        });

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.new_note, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_new_note,
                    container, false);
            return rootView;
        }
    }

}

我的XML

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.src.vicnote.NewNoteActivity"
    tools:ignore="MergeRootFrame" >

    <Button
        android:id="@+id/buttonSave"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Save" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/buttonSave"
        android:ems="10"
        android:gravity="top"
        android:inputType="textMultiLine" >

        <requestFocus />
    </EditText>
</RelativeLayout>

另外我想问你如果文字是西里尔文会怎么样?会有什么问题吗?

2 个答案:

答案 0 :(得分:0)

尝试在OnClickListener之外初始化textData

textData = (EditText) findViewById(R.id.editText);
saveButton = (Button) this.findViewById(R.id.buttonSave);
saveButton.setOnClickListener(new View.OnClickListener() { //...

答案 1 :(得分:0)

我也一直在寻找从edittext获取文本的方法。我通过以下方式解决了这个问题, String selQuantity =(((TextView)findViewById(R.id.etxtQuantity))。getText())。toString();

试试这个。它有效。