Android之下次不再提醒功能实现

时间:2022-08-31 20:47:51

在程序开发中,一般程序第一次运行时都会有用户协议、免责声明等,然后还会有一个下次不再提醒checkbox框,一旦我们勾选了下次运行程序时将不再显示此界面。本篇文章通过一个例子实现了这个效果。

首先,第一个界面

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/next_ui"
        android:onClick="next_activity"/>

</LinearLayout>

MainActivity.java

判断跳转到哪一个界面,即是否要越过界面2调到界面3

package com.example.nextnotipdemo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	
	public void next_activity(View view)
	{
		//保存数据
		SharedPreferences sharedata = getSharedPreferences(SecondActivity.DATA, 0);
		int nData = sharedata.getInt(SecondActivity.CONTENT, SecondActivity.nIsReminder);
		if (SecondActivity.REMINDER_YES == nData)
		{
			Intent intent = new Intent(MainActivity.this,SecondActivity.class);
			startActivity(intent);
			MainActivity.this.finish();
		}
		else if (SecondActivity.REMINDER_NO == nData)
		{
			Intent intent = new Intent(MainActivity.this,ThirdActivity.class);
			startActivity(intent);
			MainActivity.this.finish();
		}
		else 
		{
			Toast.makeText(MainActivity.this, "跳转错误", Toast.LENGTH_SHORT).show();
		}
		
	}
}
界面效果

Android之下次不再提醒功能实现

然后,第二个界面

判断下次启动是否显示

second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <CheckBox 
        android:id="@+id/checkbox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/next_no_tip"/>
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/next_ui"
        android:onClick="next_activity"/>

</LinearLayout>

SecondActivity.java

package com.example.nextnotipdemo;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

public class SecondActivity extends Activity
{
	public static final String DATA = "data";
	public static final String CONTENT = "content";
	
	public static int nIsReminder = 1;//是否提醒:0不提醒,1提醒,首次进入需要提醒
	public static int  REMINDER_NO = 0;
	public static int  REMINDER_YES  = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		
		//获取CheckBox实例
		CheckBox checkBox = (CheckBox)this.findViewById(R.id.checkbox);
		//绑定监听器
		checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
		{
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
			{
				if (isChecked)
				{
					nIsReminder = REMINDER_NO;
				}
				else 
				{
					nIsReminder = REMINDER_YES;
				}
				Toast.makeText(SecondActivity.this, isChecked?"选中了":"取消了选中"    , Toast.LENGTH_LONG).show();
			}
		});
	}
	
	public void next_activity(View view)
	{
		//保存数据
		SharedPreferences.Editor sharedata = getSharedPreferences(DATA, 0).edit();
		sharedata.putInt(CONTENT, nIsReminder);
		sharedata.commit();
		
		//跳转界面
		Intent intent = new Intent(SecondActivity.this,ThirdActivity.class);
		startActivity(intent);
		SecondActivity.this.finish();
	}
}
界面效果

Android之下次不再提醒功能实现

勾选下次不再显示框

Android之下次不再提醒功能实现

最后,第三个界面

third.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/ok"
        android:onClick="finish"/>

</LinearLayout>
ThirdActivity.java

package com.example.nextnotipdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class ThirdActivity extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.third);
	}
	
	public void finish(View view)
	{
		ThirdActivity.this.finish();
	}
}
界面效果

Android之下次不再提醒功能实现

关闭程序,再次运行效果

Android之下次不再提醒功能实现

下一个界面

Android之下次不再提醒功能实现

我们发现已经跳过第二个界面,直接来到第三个界面了,这说明我们设置成功了。


源码下载