【发布时间】:2017-10-27 18:40:55
【问题描述】:
我正在尝试使用 Onclick 按钮仅显示一次活动
首先我有一个活动 useheadphone 包含button 当用户点击这个button mainactivity 将启动
我的 useheadphone.java :
public class useheadphone extends Activity {
Button skip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_useheadphone);
final SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
hide();
skip = (Button)findViewById(R.id.skip);
skip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(pref.getBoolean("activity_executed", false)){
Intent toMain = new Intent(getApplicationContext(),MainActivity.class);
startActivity(toMain);
finish();
} else {
SharedPreferences.Editor ed = pref.edit();
ed.putBoolean("activity_executed", true);
ed.commit();
}
}
});
}
我的清单:
<activity
android:name=".useheadphone"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_useheadphone"
android:screenOrientation="portrait"
android:theme="@style/FullscreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
【问题讨论】:
-
只有当activity_executed为真时才会被设置为真。您需要从它为 false 时将其设置为 true,反之亦然。
-
@MikeT 非常感谢,请给我更多信息
-
@ S.Queroane:: 根据您在 Doron 的回答中留下的评论,在我看来,您正在向后设计您的应用程序。如果您希望 useheadphone 只出现一次,则让 MainActivity 启动活动并仅有条件地打开 useheadphone 活动,即当“activity_executed”为真时。
-
@Barns52 谢谢,但 Xenolion 的回答对我有用stackoverflow.com/questions/46981516/…
-
有效的方法并不总是好的。学习愉快!
标签: android android-activity sharedpreferences