我正在尝试制作一款能够吸收学生详细信息的应用。该细节的一部分是学生年龄,应该只是数字输入。如果在编辑文本字段中输入了字母,则应用程序必须指示错误。
我有下面的代码,但是似乎我对应用程序没有任何更改,因为只要我输入年龄字母,它仍然会崩溃。
// xml内容
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="40dp"
android:background="#F0F8FF"
>
<TableLayout
android:id="@+id/add_table"
android:layout_width="match_parent"
android:layout_height="606dp"
android:paddingTop="40dp">
<TableRow>
<TextView
android:layout_marginLeft="25dp"
android:padding="3dip"
android:text="Student ID:" />
<EditText
android:id="@+id/sid"
android:layout_width="190dp"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<TextView
android:layout_marginLeft="25dp"
android:padding="3dip"
android:text="First Name:" />
<EditText
android:id="@+id/fn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="150dip" />
</TableRow>
<TableRow>
<TextView
android:layout_marginLeft="25dp"
android:padding="3dip"
android:text="Last Name:" />
<EditText
android:id="@+id/ln"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="150dip" />
</TableRow>
<TableRow>
<TextView
android:layout_marginLeft="25dp"
android:padding="3dip"
android:layout_marginTop="5dp"
android:text="Gender:" />
<RadioGroup
android:id="@+id/ge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="0.9"
android:scaleY="0.9"
android:text="Male" />
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleX="0.9"
android:scaleY="0.9"
android:text="Female" />
</RadioGroup>
</TableRow>
<TableRow>
<TextView
android:layout_marginLeft="25dp"
android:padding="3dip"
android:text="Course Study:" />
<EditText
android:id="@+id/cs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="150dip" />
</TableRow>
<TableRow>
<TextView
android:layout_marginLeft="25dp"
android:inputType="number"
android:digits="0123456789"
android:padding="3dip"
android:text="Age:" />
<EditText
android:id="@+id/ag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="150dip" />
</TableRow>
<TableRow>
<TextView
android:layout_marginLeft="25dp"
android:padding="3dip"
android:text="Address:" />
<EditText
android:id="@+id/ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="150dip" />
</TableRow>
<Button
android:id="@+id/add_button"
android:layout_width="207dp"
android:layout_height="wrap_content"
android:layout_marginLeft="118dp"
android:layout_marginRight="52dp"
android:layout_marginTop="14dp"
android:padding="6dip"
android:text="Add Student" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
app:srcCompat="@mipmap/man" />
</TableLayout>
// Java
package com.user.project3;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class Addrecord extends AppCompatActivity {
DatabaseManager myDb;
EditText sid, fn, ln, cs, ag, ad;
RadioGroup radioGenderGroup;
RadioButton radioGenderButton;
Button btnAddStudent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
myDb = new DatabaseManager(this);
sid = (EditText)findViewById(R.id.sid);
fn = (EditText)findViewById(R.id.fn);
ln = (EditText)findViewById(R.id.ln);
cs = (EditText)findViewById(R.id.cs);
ag = (EditText)findViewById(R.id.ag);
ad = (EditText)findViewById(R.id.ad);
btnAddStudent = (Button)findViewById(R.id.add_button);
AddStudentRecord();
}
public void AddStudentRecord() {
btnAddStudent.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
radioGenderGroup = (RadioGroup) findViewById(R.id.ge);
int selectedid = radioGenderGroup.getCheckedRadioButtonId();
radioGenderButton = (RadioButton) findViewById(selectedid);
boolean isInserted = myDb.insertDataStudent(
Integer.parseInt(sid.getText().toString()),
fn.getText().toString(),
ln.getText().toString(),
radioGenderButton.getText().toString(),
cs.getText().toString(),
Integer.parseInt(ag.getText().toString()),
ad.getText().toString()
);
String strNumber=ag.getText().toString().trim();
if(TextUtils.isEmpty(strNumber) || Integer.parseInt(strNumber)>100){
Toast.makeText(Addrecord.this,"Please input a number",Toast.LENGTH_LONG).show();
}
if(isInserted == true)
Toast.makeText(Addrecord.this,"Data Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(Addrecord.this,"Data not Inserted",Toast.LENGTH_LONG).show();
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.screen2_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.home2) {
Intent intent = new Intent(Addrecord.this, Home.class);
startActivity(intent);
return true;
}
if (id == R.id.viewarecord) {
Intent intent = new Intent(Addrecord.this, Viewstudent.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
//崩溃日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.supriya.project3, PID: 2274
java.lang.NumberFormatException: For input string: "j"
at java.lang.Integer.parseInt(Integer.java:608)
at java.lang.Integer.parseInt(Integer.java:643)
at
com.supriya.project3.Addrecord$1.onClick(Addrecord.java:72)
at android.view.View.performClick(View.java:6891)
at
android.widget.TextView.performClick(TextView.java:12651)
at
android.view.View$PerformClick.run(View.java:26083)
at
android.os.Handler.handleCallback(Handler.java:789)
at
android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at
android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Application terminated.
答案 0 :(得分:0)
尝试一下
youredittext.addTextChangedListener(new TextWatcher()
{
@Override
public void afterTextChanged(Editable mEdit)
{
String regexStr = "^[0-9]*$";
if(your_editText.getText().toString().trim().matches(regexStr))
{
//write code here for success
}
else{
// write code here on failure
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
让我知道这是否有效
答案 1 :(得分:0)
您可以使用android:inputType =“ number”。如果还想过滤用户输入的数字,则可以添加TextChangeListener。
答案 2 :(得分:0)
myLayer
答案 3 :(得分:0)
使用此正则表达式查找字符串是否包含数字:
Pattern pattern = Pattern.compile(".*[^0-9].*");
pattern.matcher(you input in edittext).matches());