android坑——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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...