package com.example.crazywriteup.getbmi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MaleActivity extends AppCompatActivity
{
EditText enm,ehgf,ehgi,ewg;
Button btnmale;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_male);
enm = (EditText)findViewById(R.id.mleditText);
ehgf = (EditText)findViewById(R.id.mleditText2);
ehgi = (EditText)findViewById(R.id.mleditText3);
ewg = (EditText)findViewById(R.id.mleditText4);
btnmale = (Button)findViewById(R.id.btnmlsubmit);
btnmale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if(enm.getText().toString().isEmpty())
{
Toast toast = Toast.makeText(getApplicationContext(), "PLS FILL NAME",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER, 0, 0);
toast.show();
}
else
{
// Nothing
ehgf.requestFocus();
}
}
});
public boolean isNum(String val)
{
boolean check = false;
String no = "\\d*\\.?\\d+";
CharSequence inputstr = val;
Pattern pte = Pattern.compile(no,Pattern.CASE_INSENSITIVE);
Matcher matcher = pte.matcher(inputstr);
if(matcher.matches())
{
check = true;
}
return check;
}
}
}
我正在使用android number文本框(),我想将范围设置为1到7,但是我在编程代码时遇到了困难。它始终显示错误cannot return a value from a method with void result type
。我是一个初学者,对编程知识不多。
答案 0 :(得分:1)
onCreate()
方法 return void
,您正在尝试返回boolean
您需要在 isNum()
onCreate()
方法
示例代码
public class MaleActivity extends AppCompatActivity
{
EditText enm,ehgf,ehgi,ewg;
Button btnmale;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_male);
enm = (EditText)findViewById(R.id.mleditText);
ehgf = (EditText)findViewById(R.id.mleditText2);
ehgi = (EditText)findViewById(R.id.mleditText3);
ewg = (EditText)findViewById(R.id.mleditText4);
btnmale = (Button)findViewById(R.id.btnmlsubmit);
btnmale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if(enm.getText().toString().isEmpty())
{
Toast toast = Toast.makeText(getApplicationContext(), "PLS FILL NAME",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER, 0, 0);
toast.show();
}
else
{
// Nothing
ehgf.requestFocus();
}
}
});
}
public boolean isNum(String val)
{
boolean check = false;
String no = "\\d*\\.?\\d+";
CharSequence inputstr = val;
Pattern pte = Pattern.compile(no,Pattern.CASE_INSENSITIVE);
Matcher matcher = pte.matcher(inputstr);
if(matcher.matches())
{
check = true;
}
return check;
}
}