android坑——EditText获取不到值

android坑——EditText获取不到值,第1张

概述android中通过studentTextName=(EditText)findViewById(R.id.editaddusername1);获取到的EditText中输入的值为空逻辑上感觉似乎没有问题那么到底是什么问题呢?将取EditText的值放入对象中的语句放到单击事件函数中就可以获取到EditText中输入的值了要在点击按钮的同

androID中通过
studentTextname = (EditText)findVIEwByID(R.ID.editaddusername1);
获取到的EditText中输入的值为空

逻辑上感觉似乎没有问题 那么到底是什么问题呢?

将取EditText的值放入对象中的语句放到单击事件函数中就可以获取到EditText中输入的值了
要在点击按钮的同时触发取值的 *** 作

错误代码

package com.example.databack;import androID.content.Intent;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.EditText;import androIDx.appcompat.app.AppCompatActivity;import com.example.databack.entity.FamilyNumber;import com.example.databack.entity.StudentNumber;import com.example.databack.entity.TeacherNumber;public class AddActivity extends AppCompatActivity {    private EditText studentTextname , studentTextPhon;    private EditText teacherTextname , teacherTextPhon;    private EditText fimallyTextname , fimallyTextPhon;    private TeacherNumber teachers;    private StudentNumber students;    private FamilyNumber famillys;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_add);        studentTextname = (EditText)findVIEwByID(R.ID.editaddusername1);        studentTextPhon = (EditText)findVIEwByID(R.ID.editaddphon1);        fimallyTextname  = (EditText) findVIEwByID(R.ID.editaddusername2);        fimallyTextPhon = (EditText) findVIEwByID(R.ID.editaddphon2);        teacherTextname = (EditText) findVIEwByID(R.ID.editaddusername3);        teacherTextPhon = (EditText) findVIEwByID(R.ID.editaddphon3);//将EditText中写入的值获取到同时放入对象中        teachers = new TeacherNumber(teacherTextname.getText().toString() ,  teacherTextPhon.getText().toString());        students = new StudentNumber(studentTextname.getText().toString() , studentTextPhon.getText().toString());        famillys = new FamilyNumber(fimallyTextname.getText().toString() , fimallyTextPhon.getText().toString());    }//点击事件实现函数    public voID addTeacher(VIEw vIEw) {        Intent intent = new Intent();        intent.putExtra("teacher" , teachers);        setResult(1 , intent);        finish();    }}

正确代码 将取EditText的值放入对象中的语句放到单击事件函数中

package com.example.databack;import androID.content.Intent;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.EditText;import androIDx.appcompat.app.AppCompatActivity;import com.example.databack.entity.FamilyNumber;import com.example.databack.entity.StudentNumber;import com.example.databack.entity.TeacherNumber;public class AddActivity extends AppCompatActivity {    private EditText studentTextname , studentTextPhon;    private EditText teacherTextname , teacherTextPhon;    private EditText fimallyTextname , fimallyTextPhon;    private TeacherNumber teachers;    private StudentNumber students;    private FamilyNumber famillys;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_add);        studentTextname = (EditText)findVIEwByID(R.ID.editaddusername1);        studentTextPhon = (EditText)findVIEwByID(R.ID.editaddphon1);        fimallyTextname  = (EditText) findVIEwByID(R.ID.editaddusername2);        fimallyTextPhon = (EditText) findVIEwByID(R.ID.editaddphon2);        teacherTextname = (EditText) findVIEwByID(R.ID.editaddusername3);        teacherTextPhon = (EditText) findVIEwByID(R.ID.editaddphon3);    }    public voID addTeacher(VIEw vIEw) {//取值存入对象中的语句移到了这里        teachers = new TeacherNumber(teacherTextname.getText().toString() ,  teacherTextPhon.getText().toString());        students = new StudentNumber(studentTextname.getText().toString() , studentTextPhon.getText().toString());        famillys = new FamilyNumber(fimallyTextname.getText().toString() , fimallyTextPhon.getText().toString());        //老师  1        Intent intent = new Intent();        intent.putExtra("teacher" , teachers);        setResult(1 , intent);        finish();    }////    public voID addFamily(VIEw vIEw) {//        //家人   2//        intent.putExtra("student" , students);//        setResult(2 , intent);//        finish();//    }////    public voID addStudent(VIEw vIEw) {//        //学生   3//        intent.putExtra("fimally" , famillys);//        setResult(3 , intent);//        finish();//    }}
总结

以上是内存溢出为你收集整理的android坑——EditText获取不到值全部内容,希望文章能够帮你解决android坑——EditText获取不到值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1062246.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存