其他分享
首页 > 其他分享> > android坑——EditText获取不到值

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

标签:findViewById,EditText,getText,获取,toString,import,android,id
来源: https://blog.csdn.net/weixin_44735933/article/details/105622328