🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
#trim() 清空多余的空格。获取句子中,把句子前的若干个空格、或句子后的若干个空格清空掉。输出没有前后空格的句子,例外的是句子的单词间的空格保持原状态。 ##用法 Sting name= EditText.getText().toString().trim(); ##例子 * * * * * 情况一 ~~~ /** * 提交书籍信息 */ private class Submit implements View.OnClickListener { @Override public void onClick(View view) { String name = bookNmae.getText().toString().trim(); String isbn = bookIsbn.getText().toString().trim(); //使用 trim() String publishingHouse = bookPublishingHouse.getText().toString().trim(); String anthor = bookAnthor.getText().toString().trim(); boolean judgmentNull = JudgmentNull(name, isbn, publishingHouse, anthor); if (judgmentNull) { Toast.makeText(AddBookActivity.this, name+"\n"+isbn+"\n"+publishingHouse+"\n"+anthor, Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(AddBookActivity.this, "输入的内容不能为空", Toast.LENGTH_SHORT).show(); } } } ~~~ ###效果 ![](https://box.kancloud.cn/8d34592bce5445ec4ace2b57df7d1b52_401x673.jpg) * * * * * 情况二 ~~~ /** * 提交书籍信息 */ private class Submit implements View.OnClickListener { @Override public void onClick(View view) { String name = bookNmae.getText().toString().trim(); String isbn = bookIsbn.getText().toString(); //不使用 trim() String publishingHouse = bookPublishingHouse.getText().toString().trim(); String anthor = bookAnthor.getText().toString().trim(); boolean judgmentNull = JudgmentNull(name, isbn, publishingHouse, anthor); if (judgmentNull) { Toast.makeText(AddBookActivity.this, name+"\n"+isbn+"\n"+publishingHouse+"\n"+anthor, Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(AddBookActivity.this, "输入的内容不能为空", Toast.LENGTH_SHORT).show(); } } } ~~~ ###效果 ![](https://box.kancloud.cn/0df6f3e0d4a9194f7e062e6504a5c32f_390x705.jpg)