Java 为应用程序创建用户名并启动
我正在尝试为我的应用程序创建一个屏幕,允许用户创建用户名,然后启动应用程序。我已经有了一个屏幕,其中包含了我希望用户执行的所有操作,但我现在想添加一个登录屏幕,用户在进入活动屏幕之前必须使用该屏幕。我已经设计好了登录界面,还有一个单独的类可以使用。有人知道如何进行,以便MainActivity首先加载登录名吗?我假设我需要设置某种布尔标志,允许Main加载应用程序的其余部分Java 为应用程序创建用户名并启动,java,android,Java,Android,我正在尝试为我的应用程序创建一个屏幕,允许用户创建用户名,然后启动应用程序。我已经有了一个屏幕,其中包含了我希望用户执行的所有操作,但我现在想添加一个登录屏幕,用户在进入活动屏幕之前必须使用该屏幕。我已经设计好了登录界面,还有一个单独的类可以使用。有人知道如何进行,以便MainActivity首先加载登录名吗?我假设我需要设置某种布尔标志,允许Main加载应用程序的其余部分 protected void onCreate(Bundle savedInstanceState) { sup
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
setContentView(R.layout.activity_main);
Size = (SeekBar)findViewById(R.id.size);
Speed =(SeekBar)findViewById(R.id.speed);
Agility=(SeekBar)findViewById(R.id.agility);
Vision =(SeekBar)findViewById(R.id.vision);
text = (TextView) findViewById(R.id.text);
Size.setOnSeekBarChangeListener(this);
Speed.setOnSeekBarChangeListener(this);
Agility.setOnSeekBarChangeListener(this);
Vision.setOnSeekBarChangeListener(this);
}
我有一个用于登录的设置内容视图
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/login"
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">
<Button
android:text="Begin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/begin"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="124dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:layout_above="@+id/begin"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="88dp"
android:layout_marginEnd="88dp"
android:layout_marginBottom="149dp"
android:id="@+id/editText2" />
</RelativeLayout>
我有我的布局。我还有一节课,现在是空的
我是否必须在我的AndroidManifest中执行某些操作?目前,您的MainActivity是主要的启动器活动。将其更改为您的LogInACtivity,然后您的LogInACtivity将首先启动。在清单中,从MainActivity的活动标记中剪切以下代码,并将其粘贴到新创建的LogInActivity的活动标记中
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
希望这有帮助。您可以尝试启动一个活动或基于布尔值的(用户名)。因此,像普通一样启动MainActivity,并在onCreate()中检查用户是否已使用其用户名登录。如果没有,则显示登录屏幕。用户登录时,更改SharedReference值 代码
//在onCreate()中。。。
SharedReferences prefs=首选项管理器
.getDefaultSharedReferences(StartActivity.this);
if(!prefs.getBoolean(“LoggedIn”,false)){
//启动登录活动
}否则{
//做点别的
}
//在您的登录活动中,最好在用户提供有效用户名后。。。
SharedReferences prefs=首选项管理器
.getDefaultSharedReferences(StartActivity.this);
SharedReferences.Editor=prefs.edit();
//将LoggedIn设置为true
编辑器.putBoolean(“LoggedIn”,true);
编辑器.putString(“用户名”);
希望这有帮助!就整个布尔运算而言,您的思路是正确的,但仅仅检查一个局部声明的变量并不能解决这个问题。您需要在系统上实际存储数据,以便稍后进行检查,就像在连续发射期间一样
// In onCreate()...
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(StartActivity.this);
if (!prefs.getBoolean("LoggedIn", false)) {
// Launch login activity
} else {
// Do something else
}
// In your Login Activity, preferably after the user has provided a valid username...
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(StartActivity.this);
SharedPreferences.Editor editor = prefs.edit();
// Set LoggedIn to true
editor.putBoolean("LoggedIn", true);
editor.putString("username", <The username they chose>);