Toast在activity关闭后,还在显示的问题

1.toast.show();是展示的方法,toast.cancel();是取消的方法

代码:

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

    //弹出toast提示
    Toast toast = null;
    if (toast == null) {
        toast = Toast.makeText(mContext, "这里是提示", Toast.LENGTH_SHORT);
    }
    toast.show();//弹出提示
}

在activity被挂起,摧毁的时候,对弹出的toast提示进行处理.

/**
 * 处理onStop方法,
 * */
@Override
protected void onStop() {
    super.onStop();
    toast.cancel();
}


/**
 * 处理onPause方法
 * */
@Override
protected void onPause() {
    super.onPause();
    toast.cancel();
}

/**
 * 处理onDestory方法
 * */
@Override
protected void onDestroy() {
    super.onDestroy();
    toast.cancel();
}

多处使用简单的实现,自定义Toast的工具,然后再自定义的BaseActivity里面来实现

简单的Toast工具

public class ToastUtils {

    private static Toast mToast,mToastNormal;
    private static View view;

    /**
     * 普通的toast提示
     * */
    public static void showNOrmalToast(Context mContext,String message){

        ToastUtils.cancel();

        if(mToastNormal == null){
            mToastNormal = Toast.makeText(mContext, message, Toast.LENGTH_SHORT);
        }

        mToastNormal.show();

    }
    /**
     *toast取消
     */
    public static void cancel(){

        if(mToast != null){
            mToast.cancel();
            mToast = null;
        }

    }
}
以下是从SQLite数据库中获取问题和选项并在Android应用程序中显示的基本代码: 首先,创建一个SQLiteOpenHelper子类,用于管理数据库。在这个例子中,我们将问题和选项存储在不同的表格中: ```java public class DatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "quiz.db"; private static final int DATABASE_VERSION = 1; private static final String TABLE_QUESTIONS = "questions"; private static final String TABLE_OPTIONS = "options"; private static final String COLUMN_ID = "_id"; private static final String COLUMN_QUESTION = "question"; private static final String COLUMN_OPTION = "option"; private static final String COLUMN_IS_ANSWER = "is_answer"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String queryQuestions = "CREATE TABLE " + TABLE_QUESTIONS + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_QUESTION + " TEXT" + ")"; db.execSQL(queryQuestions); String queryOptions = "CREATE TABLE " + TABLE_OPTIONS + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_QUESTION + " INTEGER, " + COLUMN_OPTION + " TEXT, " + COLUMN_IS_ANSWER + " INTEGER" + ")"; db.execSQL(queryOptions); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUESTIONS); db.execSQL("DROP TABLE IF EXISTS " + TABLE_OPTIONS); onCreate(db); } public Cursor getAllQuestions() { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_QUESTIONS, null); return cursor; } public Cursor getOptionsForQuestion(int questionId) { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_OPTIONS + " WHERE " + COLUMN_QUESTION + " = ?", new String[] { String.valueOf(questionId) }); return cursor; } } ``` 接下来,在你的Activity中,调用getAllQuestions()方法获取问题,并在ListView中显示它们。当用户选择一个问题时,调用getOptionsForQuestion()方法获取相应的选项,并在RadioGroup中显示它们: ```java public class QuizActivity extends AppCompatActivity { private ListView listView; private ArrayList<String> questionsList; private ArrayAdapter<String> adapter; private RadioGroup radioGroup; private Button submitButton; private DatabaseHelper dbHelper; private int currentQuestionId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_quiz); listView = findViewById(R.id.list_view); questionsList = new ArrayList<>(); adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, questionsList); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { currentQuestionId = position + 1; radioGroup.removeAllViews(); Cursor cursor = dbHelper.getOptionsForQuestion(currentQuestionId); if (cursor.moveToFirst()) { do { String option = cursor.getString(cursor.getColumnIndex("option")); boolean isAnswer = cursor.getInt(cursor.getColumnIndex("is_answer")) == 1; RadioButton radioButton = new RadioButton(QuizActivity.this); radioButton.setText(option); radioButton.setChecked(isAnswer); radioGroup.addView(radioButton); } while (cursor.moveToNext()); } } }); radioGroup = findViewById(R.id.radio_group); submitButton = findViewById(R.id.submit_button); submitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int selectedId = radioGroup.getCheckedRadioButtonId(); if (selectedId != -1) { RadioButton radioButton = findViewById(selectedId); String selectedOption = radioButton.getText().toString(); Cursor cursor = dbHelper.getOptionsForQuestion(currentQuestionId); if (cursor.moveToFirst()) { do { String option = cursor.getString(cursor.getColumnIndex("option")); boolean isAnswer = cursor.getInt(cursor.getColumnIndex("is_answer")) == 1; if (selectedOption.equals(option)) { if (isAnswer) { Toast.makeText(QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(QuizActivity.this, "Incorrect", Toast.LENGTH_SHORT).show(); } } } while (cursor.moveToNext()); } } } }); dbHelper = new DatabaseHelper(this); Cursor cursor = dbHelper.getAllQuestions(); if (cursor.moveToFirst()) { do { String question = cursor.getString(cursor.getColumnIndex("question")); questionsList.add(question); } while (cursor.moveToNext()); } adapter.notifyDataSetChanged(); } @Override protected void onDestroy() { super.onDestroy(); if (dbHelper != null) { dbHelper.close(); } } } ``` 在这个例子中,我们将问题存储在一个ListView中,将选项存储在一个RadioGroup中。当用户选择一个选项并点击提交按钮时,我们检查它是否是正确的答案,并显示相应的消息。注意在Activity生命周期结束时关闭数据库连接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值