java - 如何添加另一个 Activity 作为登录屏幕android

标签 java android hashmap android-tabhost android-framelayout

我是 Android 新手,正在为我的第一个应用程序进行更新。更新是在开头放置一个登录屏幕。 我选择使用一个新 Activity 来检查输入的凭据并首先启动它,然后如果用户名和密码匹配,它将把用户带到主要 Activity 。 我写了代码,但它不起作用。当我将它发送到我的手机时,它给我 android 消息错误,就好像它崩溃了一样。 我很快意识到,我的老师希望将用户名和密码保存为资源字符串数组以模拟数据库,因为我们还没有学到这一点。他建议的其他选项是使用 tabHost 或 framLayout 控制 View ,我认为使用另一个 Activity 会更容易,因为到目前为止我所使用的都是 Activity 。 如果有人可以查看我的代码并告诉我我做错了什么。 另请注意,我不是要求为我解决作业,我只是想学习。 非常感谢大家。

import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    // buttons variables
    Button signIn, exit , clear;


     final HashMap<String, String> hash= new HashMap<String, String>();
     hash.put(getString(R.array.usernameArray),getString(R.array.passwordArray));

  // text boxes variables
        final EditText userName = (EditText) findViewById(R.id.usernameEditText);
        userName.setEnabled(true);

        final EditText password = (EditText) findViewById(R.id.passwordEditText);
        password.setEnabled(true);

     // initializing the signin button
        signIn = (Button) findViewById(R.id.signInButton);
        signIn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int count = 3;
        EditText usrnm =   (EditText)findViewById(R.id.usernameEditText); 
        EditText pswd = (EditText)findViewById(R.id.passwordEditText);

                if(hash.containsKey(usrnm))
                {
                    String val = hash.get(usrnm);
                    if(val.equals(pswd)){

                    Toast.makeText(getApplicationContext(), 
                                "the values are matched", 
                                Toast.LENGTH_LONG).show();
Intent openMainActivity = new Intent("com.alijaouhari.paycalculator.MainActivity");
                        startActivity(openMainActivity);
                    }
                    else if(count>0)
                    {
    Toast.makeText(getApplicationContext(), 
     "Wrong Credentials, Please check your usename and password and try again", 
                        Toast.LENGTH_LONG).show();
                        count--;
                    }

                }

            }
        }); // end signin button



        // initializing the exit button
        exit = (Button) findViewById(R.id.exitButton);
        exit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        }); // end exit button

        // initializing the clear button
        clear = (Button) findViewById(R.id.clearButton);
        clear.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
// when clicking the clear button, it sets all the text edit boxes to an empty string
                userName.setText("");
                password.setText("");


            }
        });// end clear button

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

最佳答案

您需要在 Manifest xml 文件中为您的 Activity 添加正确的 Intent 过滤器。

如果“MainActivity”是您希望在应用打开时首先启动的 Activity 的名称,请将以下内容添加到 list 文件中。请记住,您的 Intent 过滤器中的这些(即 DEFAULT 和 LAUNCHER)类别字段仅适用于应用程序中的单个 Activity 。

<activity
        android:name="com.example.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

关于java - 如何添加另一个 Activity 作为登录屏幕android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21924931/

相关文章:

android - 如何在不调用 observe() 的情况下从映射的 LiveData 的 getValue() 获得非空结果?

Java布局管理器垂直居中

java - 代号一谷歌登录问题

java - 检索类或对象的实例

android - 如果我将我的 Android 应用程序设置为系统应用程序,在恢复出厂设置后,它会从手机中删除吗?

android - 如何从 Android Studio 为特定版本的 AOSP 开发应用程序?

java - 我是否可以有一个可以同时采用 HashMap 或列表作为参数的方法?

Java HashMap 可以通过hash来访问

java - 什么是 java HashMap 并发的解释,获取/设置键或值?

java - 如何正确从rgb565转换为rgb888