android坑——EditText获取不到值
短信预约 -IT技能 免费直播动态提醒
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();
// }
}
作者:云峰的程序世界
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341