当文本部分为空白并且按下按钮时(在 android studio 中),我如何显示 toast 消息?
how can I show toast message when text section is blank and the button gets pressed (in android studio)?
我已经在 android studio 中创建了待办事项列表应用程序。我想在没有文本的情况下按下按钮时显示吐司消息。喜欢 "Please enter Text"。并防止创建任何空白列表。我有两个 java class 文件。 MainActivity 和 FileHelper
MainActivity(代码):
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;
}
}
检查此代码:
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.add_btn:
if(!TextUtils.isEmpty(ItemET.getText().toString())){
String ItemEntered = ItemET.getText().toString();
adapter.add(ItemEntered);
ItemET.setText("");
FileHelper.writeData(items, this);
Toast.makeText(this, "item Added", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Please enter Text", Toast.LENGTH_SHORT).show();
}
break;
}
}
我已经在 android studio 中创建了待办事项列表应用程序。我想在没有文本的情况下按下按钮时显示吐司消息。喜欢 "Please enter Text"。并防止创建任何空白列表。我有两个 java class 文件。 MainActivity 和 FileHelper
MainActivity(代码):
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;
}
}
检查此代码:
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.add_btn:
if(!TextUtils.isEmpty(ItemET.getText().toString())){
String ItemEntered = ItemET.getText().toString();
adapter.add(ItemEntered);
ItemET.setText("");
FileHelper.writeData(items, this);
Toast.makeText(this, "item Added", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Please enter Text", Toast.LENGTH_SHORT).show();
}
break;
}
}