我必须在学校编写我的第一个应用程序,这是我的代码:
我在setOnClickListener
部分遇到问题。它不起作用,你能帮助我并告诉我我的错误吗?
public class MainActivity extends ActionBarActivity {
TextView input, output;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (TextView) findViewById(R.id.textView2);
output = (TextView) findViewById(R.id.Eingabefeld);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
boolean Check() {
if (isNumber(input.getText().charAt(0)) == true) {
output.setText("This is a Number");
}
else {
if (isLetter(input.getText().charAt(0)) == true) {
output.setText("This is a Letter");
}
else
{
output.setText("This is a Gigit");
}
}
return true;
}
}
});
}
public boolean isNumber(char myChar) {
if ((int)myChar >= 48 && (int)myChar <= 57) {
return true;
}
else {
return false;
}
}
public boolean isLetter(char myChar) {
if ((int)myChar >=97 && (int)myChar <=122) {
return true;
}
else {
return false;
}
}
public boolean isDigit(char myChar) {
if ((int)myChar >=33 && (int)myChar <=47) {
return true;
}
else {
return false;
}
}
答案 0 :(得分:0)
为什么要在点击处理程序中创建布尔检查功能?
我认为你打算做的是:
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (isNumber(input.getText().charAt(0)) == true) {
output.setText("This is a Number");
}
else {
if (isLetter(input.getText().charAt(0)) == true) {
output.setText("This is a Letter");
}
else
{
output.setText("This is a Gigit");
}
}
}
});