字符串不会出现在Android吐司中

时间:2016-07-01 17:30:39

标签: android android-edittext android-toast

我一直在研究一个有两个edittext字段和两个微调器的活动。在EditText字段中输入的文本不会显示在我生成的吐司中,即使在微调器中完成的选择也是如此。我做错了什么?

package com.goswami.pntgo.notifierdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.*;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class    LoginScreen extends Activity {
            String Course = null;
            String Semester = null;
            String univRollNo = null;
            String name = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_screen);

    final EditText tvname = (EditText) findViewById(R.id.name);
    final EditText tvunivrn = (EditText) findViewById(R.id.univ_roll_no);
    name = tvname.getText().toString();
    univRollNo = tvunivrn.getText().toString();



    Spinner course = (Spinner)findViewById(R.id.course);
    Spinner semester = (Spinner)findViewById(R.id.semester);
    ArrayAdapter cadapter = ArrayAdapter.createFromResource(this,R.array.courses,R.layout.courses);
    ArrayAdapter sadapter = ArrayAdapter.createFromResource(this,R.array.semesters,R.layout.courses);

    course.setAdapter(cadapter);
    semester.setAdapter(sadapter);

    course.setOnItemSelectedListener(new OnItemSelectedListener(){
        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }

        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
            Course = parent.getItemAtPosition(pos).toString();

        }

    });

    semester.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Semester = parent.getItemAtPosition(position).toString();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    Button login = (Button)findViewById(R.id.button);
    login.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v){
            Toast.makeText(LoginScreen.this,(name+" Roll No:"+univRollNo+"\n"+Course+" "+Semester),Toast.LENGTH_LONG).show();
        }
    });
}

}

1 个答案:

答案 0 :(得分:2)

在将任何内容输入univRollNo之前,您正在设置tvunivrn的内容。

将此行添加到登录按钮OnClickListener的顶部:

univRollNo = tvunivrn.getText().toString();

或者只是绕过它并提取文本:

Toast.makeText(LoginScreen.this, name + " Roll No:" + tvunivrn.getText().toString() + "\n" + Course + " " + Semester, Toast.LENGTH_LONG).show();