字符串到整数不起作用
嗯...我可以在 Java(和所有其他语言)中轻松完成此操作,但是,它似乎在 Android 中不起作用。
我只是想从用户那里获取一个整数输入,按下按钮并对其进行一些简单的数学计算,然后显示它。
我正在使用 TextEdit(也尝试过 TextView)并尝试了许多不同的方法,但是没有任何效果。代码本身没有错误,如果使用常量,我可以进行数学计算并显示它。
问题在于将其解析为整数(不过,代码行似乎没问题,尽管它与我在纯java中使用的有点不同。
欢迎任何帮助 - 我确信它很简单,而且我读了很多相关的文章)有关于此的帖子,包括 Android 开发网站 - 但是...
这是简短的完整代码:
package com.bt.strint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class string_integer extends Activity {
/** Called when the activity is first created. */
int aa = 3;
int y=33;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView input = (TextView) findViewById(R.id.EditText01);
final Button btn = (Button) findViewById(R.id.Button01);
final TextView showMe = (TextView) findViewById(R.id.TextView01);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//showMe.setText("meow"); //ok
//showMe.setText(String.valueOf(y * 7)); // ok
y = Integer.parseInt((String) input.getText());
showMe.setText(String.valueOf(y));
}
});
}
}
Humm... I can do this easily in Java (and all other languages) but, it doesn't seem to work in Android.
I simply want to get an integer number input from the user, press a button and do some simple math on it, then display it.
I'm using a TextEdit (also tried a TextView) and have tried many different approaches but, none work. The code itself has no errors and I can do the math and display it if using constants.
The problem is in either parsing it to an integer (though, the code-line seems ok, even though it's a tad different than what I use in pure java.
Any help is welcome - I'm sure it's simple and I read many related posts for this, including the Android Dev site - but...
Here's the short complete code:
package com.bt.strint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class string_integer extends Activity {
/** Called when the activity is first created. */
int aa = 3;
int y=33;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView input = (TextView) findViewById(R.id.EditText01);
final Button btn = (Button) findViewById(R.id.Button01);
final TextView showMe = (TextView) findViewById(R.id.TextView01);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//showMe.setText("meow"); //ok
//showMe.setText(String.valueOf(y * 7)); // ok
y = Integer.parseInt((String) input.getText());
showMe.setText(String.valueOf(y));
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想使用
input.getText().toString()
,而不是(String) input.getText()
;你不能只将 CharSequence 转换为 String。You want to use
input.getText().toString()
, instead of(String) input.getText()
; you can't just cast a CharSequence to String.