来自EditText的输入仅接受提示而不是输入的文本

时间:2016-04-05 13:28:29

标签: java android android-studio

我有一个基本的患者详细信息活动,可以获取患者详细信息并将其保存到文本文件中。目前它的工作正常,只是它从编辑文本字段接收的输入是来自编辑文本的提示,而不是我试图输入的实际信息。我的代码对我来说似乎很完美,我看不出我错在哪里:

public class PatientDetails extends AppCompatActivity {

public EditText IDNum, name, DOB, weight, height;
public String   ID, dob, wght, hght, nme;
public Button   b;
public TextView t;
private FileUtility myFile = new FileUtility();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_patient_details);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    IDNum = ((EditText) findViewById(R.id.IDnum));
    DOB = ((EditText) findViewById(R.id.DOB));
    weight = ((EditText) findViewById(R.id.Weight));
    height = ((EditText) findViewById(R.id.Height));
    name = ((EditText) findViewById(R.id.name));
    b = ((Button) findViewById(R.id.submit));

    t = ((TextView) findViewById(R.id.result));

    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ID = IDNum.getText().toString();
            dob = DOB.getText().toString();
            wght = weight.getText().toString();
            hght = height.getText().toString();
            nme = name.getText().toString();

            myFile.createFile(getApplicationContext(), "test");
            myFile.writeLine(ID);
            myFile.writeLine(dob);
            myFile.writeLine(wght);
            myFile.writeLine(hght);
            myFile.writeLine(nme);
            t.setText(myFile.readAll());
        }

    });


    }

}

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_patient_details"
tools:context="com.example.user.filetest.PatientDetails">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:hint="Name"
    android:ems="10"
    android:id="@+id/name"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/IDnum"
    android:layout_below="@+id/name"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:hint="Patient ID number" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/Height"
    android:layout_below="@+id/IDnum"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:hint="Height"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/Weight"
    android:hint="Weight"
    android:layout_below="@+id/Height"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="date"
    android:ems="10"
    android:id="@+id/DOB"
    android:layout_below="@+id/Weight"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:hint="DOB" />

<Button
    android:hint="Submit"
    android:id="@+id/submit"
    android:layout_width="300dp"
    android:layout_height="70dp"
    android:layout_below="@+id/DOB"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="70dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/result"
    android:layout_below="@+id/submit"
    android:layout_marginTop="53dp"
    android:layout_alignLeft="@+id/submit"
    android:layout_alignStart="@+id/submit"
    android:layout_alignRight="@+id/submit"
    android:layout_alignEnd="@+id/submit"
    android:textSize="7dp" />

/////文件实用程序/////////

public class FileUtility {

private File root;
private File file;

public FileUtility() {
    root = Environment.getExternalStorageDirectory();
}

public void createFile(Context context, String fileName) {
    try {
        if (root.canWrite()) {
            file = new File(root, "//" + fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
        }
        else
        {
            file = new File(context.getFilesDir(), "//" + fileName); // File(root, "//" + fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
        }
    } catch (IOException e) {
        Log.e("Error", "fail to create a new file");
    }

}


public String readAll() {
    StringBuilder returnString = new StringBuilder();
    try {
        BufferedReader in;
        FileReader datawriter = new FileReader(file);
        in = new BufferedReader(datawriter);
        if (file.exists()) {
            String str = null;
            while((str=in.readLine())!=null)
            {
                returnString.append(str + "\n");
            }
        }
        in.close();
    } catch (IOException e) {
        Log.e("Error", "fail to write file");
    }
    return returnString.toString();
}

public void writeLine(String message) {
    try {
        BufferedWriter out;
        FileWriter datawriter = new FileWriter(file,true);
        out = new BufferedWriter(datawriter);
        if (file.exists()) {
            out.write(message + "\n");
            out.flush();
        }
        out.close();
    } catch (IOException e) {
        Log.e("Error", "fail to write file");
    }
}

}

2 个答案:

答案 0 :(得分:-1)

尝试再次搜索您的代码。您无法从getText()方法获取提示文本...除非您在EditText元素中将该提示硬预填充为文本。

答案 1 :(得分:-1)

尝试这个:

 IDNum = (EditText) findViewById(R.id.IDnum);