gpt4 book ai didi

android - 字符串不显示在 android toast 中

转载 作者:行者123 更新时间:2023-11-29 17:17:33 25 4
gpt4 key购买 nike

我一直在从事一项 Activity ,该 Activity 有两个编辑文本字段和两个微调器。在 EditText 字段中输入的文本不会显示在我生成的 toast 中,即使在微调器中完成的选择也会显示。我做错了什么?

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

最佳答案

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

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

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

或者只是规避它并提取文本:

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

关于android - 字符串不显示在 android toast 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38151035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com