java - 如何停止允许字符串或字符的功能?(安卓)
问题描述
我正在创建一个库存应用程序,允许用户输入具有三个不同信息字段的输入项目。这些字段是项目名称、项目所有者和项目编号。
但是,当我在项目编号字段中输入字符串或字符时,应用程序会崩溃。我想通过不允许仅在此字段中输入字符来防止这种崩溃。我已经尝试了几个修复,但到目前为止似乎没有一个有效。
有没有专门检查字符串类型并防止使用它们?
这是我的 addItem 函数:
public class AddItem extends AppCompatActivity {
// Setting up variable names for XML object items
EditText mItemName;
EditText mOwnerName;
EditText mItemNum;
Button mAddButton;
@Override
//Initializing object variables and the addButton setOnClickListener
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_item);
mItemName = findViewById(R.id.itemName_input);
mOwnerName = findViewById(R.id.owner_input);
mItemNum = findViewById(R.id.num_input);
mAddButton = findViewById(R.id.add_button);
//This click listener sets up functionality for the add item button. The addItem method is called from the MyDBHelper class.
mAddButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//TESTING
String itemName = mItemName.getText().toString().trim();
String itemOwner = mOwnerName.getText().toString().trim();
String itemNum = mItemNum.getText().toString().trim();
//This IF/ELSE branch checks to see if any fields in AddItem are empty. If so, an error message displays. Else, it is entered into the database.
if(itemName.equals("")||itemOwner.equals("")||itemNum.equals("")) {
Toast.makeText(AddItem.this, "Please enter data to all fields!", Toast.LENGTH_SHORT).show();
}
else {
MyDBHelper myDatabase = new MyDBHelper(AddItem.this);
myDatabase.addItem(mItemName.getText().toString().trim(), mOwnerName.getText().toString().trim(), Integer.valueOf(mItemNum.getText().toString().trim()));
//This will add functionality to return to the DisplayInventory Screen after pressing button.
Intent intent = new Intent(AddItem.this, DisplayInventory.class);
startActivity(intent);
finish();
}
}
});
}
}
这是该函数正在使用的数据库:
public class MyDBHelper extends SQLiteOpenHelper {
// Setting up all variables that make up the columns in the database.
private Context context;
private static final String DATABASE_NAME = "InventoryList.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "my_list";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TITLE = "item_name";
private static final String COLUMN_OWNER = "item_owner";
private static final String COLUMN_NUM = "item_num";
MyDBHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
//Initializing the Database
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String query =
"CREATE TABLE " + TABLE_NAME +
" (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_TITLE + " TEXT, " +
COLUMN_OWNER + " TEXT, " +
COLUMN_NUM + " INTEGER);";
sqLiteDatabase.execSQL(query);
}
@Override
//This will make sure to erase the table if one already exists.
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
//This method is adding functionality to add the item name, owner and number of items.
void addItem(String itemName, String itemOwner, int numItem) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_TITLE, itemName);
cv.put(COLUMN_OWNER, itemOwner);
cv.put(COLUMN_NUM, numItem);
long result = db.insert(TABLE_NAME, null, cv);
if (result == -1) {
Toast.makeText(context, "Failed", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(context, "Item Added to Inventory!", Toast.LENGTH_SHORT).show();
}
}
//This method sets up the functionality to search the entire database and read the data.
Cursor readAllData() {
String query = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
if (db != null) {
cursor = db.rawQuery(query, null);
}
return cursor;
}
//This method allows the user to update the item's data. Values that will be updated are item name, owner and number of items.
void updateData(String row_id, String item_name, String item_owner, String item_num) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_TITLE, item_name);
cv.put(COLUMN_OWNER, item_owner);
cv.put(COLUMN_NUM, item_num);
long result = db.update(TABLE_NAME, cv, "_id=?", new String[]{row_id});
}
void deleteItem(String row_id) {
SQLiteDatabase db = this.getWritableDatabase();
long result = db.delete(TABLE_NAME, "_id=?", new String[]{row_id});
}
void destroyInventory() {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_NAME);
}
}
解决方案
您需要android:inputType="number"
在 xml 中为EditText
for使用属性itemNumber
。这样用户将无法在该输入字段中输入任何字符。
推荐阅读
- xcode - UIAutomation 选项未列在 Instrument 的 XCode10 列表中
- java - 将 bbcode 转换为特殊格式
- cordova - Cordova InAppBrowser 不会在外部打开 url
- spring-cloud-stream - Spring Cloud 流式传输 kinesis binder 消费者组 - kcl 与 dynomodbstore
- typescript - 角度不从后端服务器读取令牌
- javascript - 如何匹配原始字符串中的字符串?
- spring-amqp - 使用弹簧兔,弹簧amqp。如何在客户端机器内收集rabbitMQ指标
- c# - 不使用循环获取字典对象的第一个值
- angular - 如何在 ngsubmit 方法中获取预填充的表单字段值?
- reactjs - 为什么我的搜索功能没有通过?