gpt4 book ai didi

java - 我们可以制作两个启动器 Activity 并根据某些条件启动一个吗?

转载 作者:行者123 更新时间:2023-12-01 14:34:42 25 4
gpt4 key购买 nike

我的启动器 Activity 是 loginActivity,我希望当用户选择一所大学并第二次打开应用程序时,他应该被移动到另一个 Activity TestYip(而不是登录 Activity )。也就是说,一旦用户登录,启动器 Activity 应该会改变。为此,我在 Select_Collage Activity 中创建了一个函数 getCollege,该 Activity 从 loginActivity 调用。
但它不工作..给出的代码:

  • 选择_拼贴
                package notes.test.firebase;

    import java.util.ArrayList;
    import java.util.HashMap;

    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Build;
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.support.v7.app.AppCompatActivity;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.view.View;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.TextView;


    public class Select_Collage extends AppCompatActivity {

    // List view
    public ListView lv;
    public TextView tv;
    public String str;


    // Listview Adapter
    ArrayAdapter<String> adapter;

    // Search EditText
    EditText inputSearch;

    final String products[] = {"Jaypee university Guna", "Delhi university", "Graphics era", "UPES",
    "Amity university", "Saradha university",
    "ITM gwalior", "RKDF university", "Indraprast university", "IIT delhi"};


    // ArrayList for Listview
    ArrayList<HashMap<String, String>> productList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.collage);
    if (Build.VERSION.SDK_INT >= 21) {
    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
    }
    // Listview Data
    /* final String products[] = {"Jaypee university Guna", "Delhi university", "Graphics era", "UPES",
    "Amity university", "Saradha university",
    "ITM gwalior", "RKDF university", "Indraprast university", "IIT delhi"};

    */
    lv = (ListView) findViewById(R.id.list_view);


    inputSearch = (EditText) findViewById(R.id.inputSearch);

    // Adding items to listview
    adapter = new ArrayAdapter<String>(this, R.layout.college_selection_text_view, R.id.product_name, products);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
    long arg3){

    tv = (TextView) arg1.findViewById(R.id.product_name);

    str = tv.getText().toString().trim();

    if (str.equals(products[0]))

    {
    Intent int0 = new Intent(Select_Collage.this, TestYip.class);
    startActivity(int0);
    }

    else if(str.equals(products[1])) {
    Intent int1 = new Intent(Select_Collage.this, MainListDisplay.class);
    startActivity(int1);

    }
    }
    });
    /**
    * Enabling Search Filter
    * */
    inputSearch.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
    // When user changed the Text
    Select_Collage.this.adapter.getFilter().filter(cs);
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
    int arg3) {
    // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable arg0) {
    // TODO Auto-generated method stub
    }


    });
    }

    public void getCollege () {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = preferences.edit();
    if (str.equals(products[0]))
    {
    editor.putInt("key", 1);
    editor.apply();
    //Intent int0 = new Intent(getAppl,TestYip.class);
    //Intent int0 = new Intent(getApplicationContext(),TestYip.class);
    //startActivity(int0);
    } else {
    editor.putInt("key", 0);
    editor.apply();
    }
    }
    }
  • 登录 Activity
    package notes.test.firebase;

    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.support.annotation.NonNull;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.text.TextUtils;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ProgressBar;
    import android.widget.Toast;

    import com.google.android.gms.tasks.OnCompleteListener;
    import com.google.android.gms.tasks.Task;
    import com.google.firebase.auth.AuthResult;
    import com.google.firebase.auth.FirebaseAuth;

    public class LoginActivity extends AppCompatActivity {

    private EditText inputEmail, inputPassword;
    private FirebaseAuth auth;
    private ProgressBar progressBar;
    private Button btnSignup, btnLogin, btnReset;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Get Firebase auth instance
    auth = FirebaseAuth.getInstance();

    if (auth.getCurrentUser() != null ) {
    Select_Collage s = new Select_Collage();
    //s.getCollege();
    //startActivity(new Intent(LoginActivity.this, MainActivity.class));
    s.getCollege();
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    int i = preferences.getInt("key",0);
    ///startActivity(new Intent(LoginActivity.this, MainActivity.class));
    if (i==1)
    {
    startActivity(new Intent(LoginActivity.this, TestYip.class));
    }
    else
    startActivity(new Intent(LoginActivity.this, MainActivity.class));


    }

    // set the view now
    setContentView(R.layout.activity_login);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    inputEmail = (EditText) findViewById(R.id.email);
    inputPassword = (EditText) findViewById(R.id.password);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    btnSignup = (Button) findViewById(R.id.btn_signup);
    btnLogin = (Button) findViewById(R.id.btn_login);
    btnReset = (Button) findViewById(R.id.btn_reset_password);

    //Get Firebase auth instance
    auth = FirebaseAuth.getInstance();

    btnSignup.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    startActivity(new Intent(LoginActivity.this, SignupActivity.class));
    }
    });

    btnReset.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    startActivity(new Intent(LoginActivity.this, ResetPasswordActivity.class));
    }
    });

    btnLogin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    String email = inputEmail.getText().toString();
    final String password = inputPassword.getText().toString();

    if (TextUtils.isEmpty(email)) {
    Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
    return;
    }

    if (TextUtils.isEmpty(password)) {
    Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
    return;
    }

    progressBar.setVisibility(View.VISIBLE);

    //authenticate user
    auth.signInWithEmailAndPassword(email, password)
    .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
    // If sign in fails, display a message to the user. If sign in succeeds
    // the auth state listener will be notified and logic to handle the
    // signed in user can be handled in the listener.
    progressBar.setVisibility(View.GONE);
    if (!task.isSuccessful()) {
    // there was an error
    if (password.length() < 6) {
    inputPassword.setError(getString(R.string.minimum_password));
    } else {
    Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
    }
    } else {
    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
    startActivity(intent);
    finish();
    }
    }
    });
    }
    });
    }
    }
  • 安卓 list
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="notes.test.firebase">

    <uses-permission android:name="android.permission.INTERNET" />

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
    android:name=".LoginActivity"
    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:label="@string/title_activity_profile"
    android:theme="@style/AppTheme.NoActionBar">




    </activity>
    <activity
    android:name=".SignupActivity"
    android:label="@string/title_activity_login"
    android:theme="@style/AppTheme.NoActionBar" />
    <activity
    android:name=".ResetPasswordActivity"
    android:label="@string/title_activity_reset_password"
    android:theme="@style/AppTheme.NoActionBar" />
    <activity
    android:name=".TestYip"
    android:label="@string/title_test"
    android:theme="@style/AppTheme.NoActionBar" />
    <activity android:name=".Computer_Notes" />
    <activity
    android:name=".MainListDisplay"
    android:label="@string/Select_Subject"
    android:theme="@style/AppTheme.NoActionBar" >
    <meta-data
    android:name="android.app.default_searchable"
    android:value=".SearchResulltsActivity" />
    </activity>
    <activity
    android:name=".Select_Collage"
    android:label="@string/Select_Collage"
    android:theme="@style/AppTheme.NoActionBar">
    <meta-data
    android:name="android.app.default_searchable"
    android:value=".SearchResulltsActivity" />
    </activity>

    <meta-data
    android:name="android.app.searchable"
    android:resource="@xml/searchable" />

    <activity android:name=".SearchResulltsActivity"
    android:theme="@style/AppTheme.NoActionBar">
    <meta-data android:name="android.support.PARENT_ACTIVITY"
    android:value=".MainActivity"/>
    <intent-filter>
    <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    <meta-data
    android:name="android.app.searchable"
    android:resource="@xml/searchable" />
    </activity>
    </application>

    </manifest>

  • 请告诉我如何根据条件从一项 Activity 转移到另一项 Activity 。

    最佳答案

    不要使用 LoginActivity 作为您的启动器 Activity 。在您的 LoginActivity 中,将一些标志值保存到 SharedPreferences表示用户已登录,然后在您的其他 Activity 的 onCreate 中(将此设置为启动器),检查此值,如果不存在,则转到 LoginActivity。

    关于java - 我们可以制作两个启动器 Activity 并根据某些条件启动一个吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39776536/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com