java - 当文本部分为空白并且按下按钮时(在 android studio 中),如何显示 toast 消息?

标签 java android android-toast

我已经在 android studio 中创建了待办事项列表应用程序。我想在没有文本的情况下按下按钮时显示 toast 消息。比如“请输入文字”。并防止创建任何空白列表。我有两个java类文件。 MainActivity 和 FileHelper

主要 Activity (代码):

public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

private EditText ItemET;
private Button btn;
private ListView itemList;

private ArrayList<String> items;
private ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ItemET = findViewById(R.id.item_edit_text);
    btn = findViewById(R.id.add_btn);
    itemList = findViewById(R.id.item_list);

    items = FileHelper.readData(this);

    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
    itemList.setAdapter(adapter);

    btn.setOnClickListener(this);
    itemList.setOnItemClickListener(this);
}

@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.add_btn:
            String ItemEntered = ItemET.getText().toString();
            adapter.add(ItemEntered);
            ItemET.setText("");
            FileHelper.writeData(items, this);
            Toast.makeText(this, "item Added", Toast.LENGTH_SHORT).show();

            break;


    }


}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    items.remove(position);
    adapter.notifyDataSetChanged();
    Toast.makeText(this, "Deleted", Toast.LENGTH_SHORT).show();
}

文件助手(代码):

public class FileHelper {

public static final String FILENAME = "listinfo.dat";

public static void writeData(ArrayList<String> items, Context context){

    try {
        FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(items);
        oos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

    public static ArrayList<String> readData(Context context) {

        ArrayList<String> itemList = null;
        try {
            FileInputStream fis = context.openFileInput(FILENAME);
            ObjectInputStream ois = new ObjectInputStream(fis);
            itemList = (ArrayList<String>) ois.readObject();
        } catch (FileNotFoundException e) {

            itemList = new ArrayList<>();


            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return itemList;

    }

最佳答案

看看下面的代码。

@Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.add_btn:
                String ItemEntered = ItemET.getText().toString();
                if(ItemEntered.trim().isEmpty()){
                    Toast.makeText(getApplicationContext(), "Please Enter some detail", Toast.LENGTH_LONG).show();
                } else {
                      adapter.add(ItemEntered);
                      ItemET.setText("");
                      FileHelper.writeData(items, this);
                      Toast.makeText(this, "item Added", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

关于java - 当文本部分为空白并且按下按钮时(在 android studio 中),如何显示 toast 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58552639/

相关文章:

android sqlite按钮不可点击

android - (关闭)在扩展 SQLiteOpenHelper 的类中创建 toast 消息

java - matches() 方法的模式匹配无法正常工作

java - 无法使用 REST 服务绘制 Google 图表

java - StringBuilder - 重置或创建一个新的

java - 如何获取特定缓存条目所在的 infinispan 节点的 IP 地址

android - 将 fabric 集成到现有项目中——如何解决 "No Android modules detected"?

android - Android按下按钮可长时间播放声音

android - Toast 未显示并出错

java - Android 设备有多少个核心?我可以使用多少个线程来获得最佳性能?