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获取不到值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)