Android 无法在first edittext字段上设置焦点?
我无法将焦点设置在first edittext字段(除下面的3个字段外)。代码为:Android 无法在first edittext字段上设置焦点?,android,android-edittext,numberformatexception,Android,Android Edittext,Numberformatexception,我无法将焦点设置在first edittext字段(除下面的3个字段外)。代码为: k1_e=(EditText)findViewById(R.id.k1_editText); k1_e.requestFocusFromTouch(); try{ k1=Float.parseFloat(k1_e.getText().toString()); }catch(Exception e) { Log.e("logtag", "Exception: " + e.toString()); } k2_
k1_e=(EditText)findViewById(R.id.k1_editText);
k1_e.requestFocusFromTouch();
try{
k1=Float.parseFloat(k1_e.getText().toString());
}catch(Exception e) {
Log.e("logtag", "Exception: " + e.toString());
}
k2_e=(EditText)findViewById(R.id.k2_editText);
try{
k2=Float.parseFloat(k2_e.getText().toString());
}catch(Exception e) {
Log.e("logtag", "Exception: " + e.toString());
}
al_e=(EditText)findViewById(R.id.al_editText);
try{
al=Float.parseFloat(al_e.getText().toString());
}catch(Exception e) {
Log.e("logtag", "Exception: " + e.toString());
}
alconst_e=(EditText)findViewById(R.id.al_const_editText);
try{
al_const=Float.parseFloat(alconst_e.getText().toString());
}catch(Exception e) {
Log.e("logtag", "Exception: " + e.toString());
}
12-29 08:10:05.241: E/logtag(769): Exception: java.lang.NumberFormatException: Invalid float: ""
12-29 08:10:05.261: E/logtag(769): Exception: java.lang.NumberFormatException: Invalid float: ""
12-29 08:10:05.261: E/logtag(769): Exception: java.lang.NumberFormatException: Invalid float: ""
12-29 08:10:05.271: E/logtag(769): Exception: java.lang.NumberFormatException: Invalid float: ""
int flag = 0;
getView()
{
if(flag == 0)
{
k1_e.requestFocusFromTouch();flag = 1;
}
string abc = k1_e.getText().toString()
if(abc != null && !abc.equals(""))
{
k1=Float.parseFloat(k1_e.getText().toString());
}
else
{
k1 = 0.0;
}
}
当应用程序在emulator上执行时,它会自动将焦点设置为last edittext字段,并引发异常:
k1_e=(EditText)findViewById(R.id.k1_editText);
k1_e.requestFocusFromTouch();
try{
k1=Float.parseFloat(k1_e.getText().toString());
}catch(Exception e) {
Log.e("logtag", "Exception: " + e.toString());
}
k2_e=(EditText)findViewById(R.id.k2_editText);
try{
k2=Float.parseFloat(k2_e.getText().toString());
}catch(Exception e) {
Log.e("logtag", "Exception: " + e.toString());
}
al_e=(EditText)findViewById(R.id.al_editText);
try{
al=Float.parseFloat(al_e.getText().toString());
}catch(Exception e) {
Log.e("logtag", "Exception: " + e.toString());
}
alconst_e=(EditText)findViewById(R.id.al_const_editText);
try{
al_const=Float.parseFloat(alconst_e.getText().toString());
}catch(Exception e) {
Log.e("logtag", "Exception: " + e.toString());
}
12-29 08:10:05.241: E/logtag(769): Exception: java.lang.NumberFormatException: Invalid float: ""
12-29 08:10:05.261: E/logtag(769): Exception: java.lang.NumberFormatException: Invalid float: ""
12-29 08:10:05.261: E/logtag(769): Exception: java.lang.NumberFormatException: Invalid float: ""
12-29 08:10:05.271: E/logtag(769): Exception: java.lang.NumberFormatException: Invalid float: ""
int flag = 0;
getView()
{
if(flag == 0)
{
k1_e.requestFocusFromTouch();flag = 1;
}
string abc = k1_e.getText().toString()
if(abc != null && !abc.equals(""))
{
k1=Float.parseFloat(k1_e.getText().toString());
}
else
{
k1 = 0.0;
}
}
错误是什么。我是初学者。请帮助您的k1_e.getText()。toString()返回的
字符串不能转换为浮点值
所有人都试试这个:
k1_e=(EditText)findViewById(R.id.k1_editText);
k1_e.requestFocusFromTouch();
try{
k1=Float.parseFloat(k1_e.getText().toString());
}catch(Exception e) {
Log.e("logtag", "Exception: " + e.toString());
}
k2_e=(EditText)findViewById(R.id.k2_editText);
try{
k2=Float.parseFloat(k2_e.getText().toString());
}catch(Exception e) {
Log.e("logtag", "Exception: " + e.toString());
}
al_e=(EditText)findViewById(R.id.al_editText);
try{
al=Float.parseFloat(al_e.getText().toString());
}catch(Exception e) {
Log.e("logtag", "Exception: " + e.toString());
}
alconst_e=(EditText)findViewById(R.id.al_const_editText);
try{
al_const=Float.parseFloat(alconst_e.getText().toString());
}catch(Exception e) {
Log.e("logtag", "Exception: " + e.toString());
}
12-29 08:10:05.241: E/logtag(769): Exception: java.lang.NumberFormatException: Invalid float: ""
12-29 08:10:05.261: E/logtag(769): Exception: java.lang.NumberFormatException: Invalid float: ""
12-29 08:10:05.261: E/logtag(769): Exception: java.lang.NumberFormatException: Invalid float: ""
12-29 08:10:05.271: E/logtag(769): Exception: java.lang.NumberFormatException: Invalid float: ""
int flag = 0;
getView()
{
if(flag == 0)
{
k1_e.requestFocusFromTouch();flag = 1;
}
string abc = k1_e.getText().toString()
if(abc != null && !abc.equals(""))
{
k1=Float.parseFloat(k1_e.getText().toString());
}
else
{
k1 = 0.0;
}
}
做这个,做个
int标志=0代码>gobal到您的班级。
当您第一次执行getview时,将标志设为1
像这样:
k1_e=(EditText)findViewById(R.id.k1_editText);
k1_e.requestFocusFromTouch();
try{
k1=Float.parseFloat(k1_e.getText().toString());
}catch(Exception e) {
Log.e("logtag", "Exception: " + e.toString());
}
k2_e=(EditText)findViewById(R.id.k2_editText);
try{
k2=Float.parseFloat(k2_e.getText().toString());
}catch(Exception e) {
Log.e("logtag", "Exception: " + e.toString());
}
al_e=(EditText)findViewById(R.id.al_editText);
try{
al=Float.parseFloat(al_e.getText().toString());
}catch(Exception e) {
Log.e("logtag", "Exception: " + e.toString());
}
alconst_e=(EditText)findViewById(R.id.al_const_editText);
try{
al_const=Float.parseFloat(alconst_e.getText().toString());
}catch(Exception e) {
Log.e("logtag", "Exception: " + e.toString());
}
12-29 08:10:05.241: E/logtag(769): Exception: java.lang.NumberFormatException: Invalid float: ""
12-29 08:10:05.261: E/logtag(769): Exception: java.lang.NumberFormatException: Invalid float: ""
12-29 08:10:05.261: E/logtag(769): Exception: java.lang.NumberFormatException: Invalid float: ""
12-29 08:10:05.271: E/logtag(769): Exception: java.lang.NumberFormatException: Invalid float: ""
int flag = 0;
getView()
{
if(flag == 0)
{
k1_e.requestFocusFromTouch();flag = 1;
}
string abc = k1_e.getText().toString()
if(abc != null && !abc.equals(""))
{
k1=Float.parseFloat(k1_e.getText().toString());
}
else
{
k1 = 0.0;
}
}
k1_e.getText().toString()返回“”无法转换为Floatedittext.requestFocus();按程序写这个。。为什么要使用touchfocus..问题是“abc”将为空,因为我无法在k1文本框上获得焦点。你能帮我吗?你在用k1_e.requestFocusFromTouch()吗;在您的自定义视图中??现在我可以向k1输入数据,但字段没有聚焦。你的意思是如果(flag==0){k1_e.requestFocusFromTouch();flag=1;}代码没有正确执行请使用requestFocus()而不是requestFocusFromTouch(),然后告诉我