对于禁用Switch button
屏幕,我正在使用splash
(如果其禁用splash
上的切换按钮正常工作)。但我不能这样做。我试试android-activity-alias
但我没有取得任何成功。请帮我。 (我也在使用Android-Studio 2.3.0
)。这是我的代码
这是我的Switch Button布局
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:background="@drawable/bg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="@string/switchButtonSplash"/>
<LinearLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="fill"
android:orientation="horizontal">
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="@android:color/darker_gray"/>
</LinearLayout>
</ScrollView>
这是我的Switch Button编程
package ahmer.afzal.myproject;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Switch;
public class SettingsActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Switch toggle = (Switch) findViewById(R.id.switch1);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
startActivity(new Intent(SettingsActivity.this, MainActivity.class));
finish();
}
else
{
startActivity(new Intent(SettingsActivity.this, SplashActivity.class));
finish();
}
}
});
setContentView(R.layout.setting_app_bar_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null)
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == android.R.id.home)
{
finish();
}
return super.onOptionsItemSelected(item);
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ahmer.afzal.myproject"
android:versionCode="550"
android:versionName="19.00">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<application
android:exported="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
<activity
android:name=".SplashActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
</activity>
</application>
</manifest>
答案 0 :(得分:0)
您无法根据应用上次打开时切换按钮的状态做出决定,因为切换按钮不会保留其状态,您必须在用户更改时将其保存在某处。
在这种情况下,您可以执行的是在sharedPreferences
和onCreate()
启动器活动中保存切换按钮的状态,检查该状态并做出相应的决定。但在这种情况下,如果您的启动器活动是Splash,那么在您进行这些检查之前,它将至少显示一次,直到您完成这些检查。
P.S:您无法更改清单中声明的启动器活动。
答案 1 :(得分:0)
首先我创建了新的 LauncherActivity 并删除了之前创建的 SettingsActivity
LauncherActivity
package="ahmer.afzal.myproject"
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
public class LauncherActivity extends Activity
{
public static String TRANSPARENT = "transparent";
private SharedPreferences sharedPref;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
sharedPref = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
if(!sharedPref.getBoolean(TRANSPARENT, false))
{
startActivity(new Intent(LauncherActivity.this, SplashActivity.class));
}
else
{
startActivity(new Intent(LauncherActivity.this, MainActivity.class));
}
finish();
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ahmer.afzal.myproject"
android:versionCode="550"
android:versionName="19.00">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<application
android:exported="true"
android:noHistory="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
<activity
android:name=".LauncherActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".SplashActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
</activity>
</application>
</manifest>