微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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();
//    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐