如何添加另一个活动作为登录屏幕android

问题描述 投票:0回答:1

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

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;
}

}
java android hashmap android-tabhost android-framelayout
1个回答
1
投票

您需要在Manifest xml文件中为您的活动添加正确的intent过滤器。

如果“MainActivity”是您要在应用程序打开时首先启动的活动的名称,请将以下内容添加到清单文件中。请记住,intent-filter中的这些(即DEFAULT和LAUNCHER)类别字段仅适用于应用程序中的单个活动。

<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>
© www.soinside.com 2019 - 2024. All rights reserved.