我的登录表单在Android中无效。可能是什么原因?

时间:2018-01-11 09:30:28

标签: java android login passwords

我正在为应用创建一个登录页面,我正在为该应用创建一个登录屏幕。但是,我的登录根本不起作用。 所以,就我而言,我的代码非常好。但是当按下“登录”按钮时,为什么没有做任何事情。错误是什么?即使我输入了正确的用户名和密码,也没有打开任何面包或任何其他页面!

public class MainPage extends AppCompatActivity {

    public EditText uName, pass;
    Button loginBtn;

    //ImageView myImageView = (ImageView) findViewById(R.id.mybackground);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_page);
        setTitle("Q-GAX");
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.abs_layout);

        loginBtn = (Button) findViewById(R.id.login_button);
        uName = (EditText) findViewById(R.id.uName);
        pass = (EditText) findViewById(R.id.pass);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (uName.getText().toString().equals("admin") && pass.getText().toString().equals("pass")) {
                    Intent myIntent = new Intent(v.getContext(),
                            TasksPanel.class);
                    startActivityForResult(myIntent, 0);
                    finish();
                } else {
                    Toast.makeText(getApplicationContext(), "Incorrect Username or Password", Toast.LENGTH_SHORT);
                }     
            }
        });
        //myImageView.setBackgroundResource(R.drawable.progressanimation);

        //AnimationDrawable frameAnimation = (AnimationDrawable) myImageView.getBackground();

        // Start the animation (looped playback by default).
        //frameAnimation.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.mainmenu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.leaveMode) {
            Toast.makeText(getApplicationContext(), "Leave Mode", Toast.LENGTH_SHORT).show();
        }
        if (id == R.id.officeMode) {
            Toast.makeText(getApplicationContext(), "Office Mode", Toast.LENGTH_SHORT).show();
        }
        if (id == R.id.meetingMode) {
            Toast.makeText(getApplicationContext(), "Meeting Mode", Toast.LENGTH_SHORT).show();
        }
        if (id == R.id.faq) {
            Toast.makeText(getApplicationContext(), "App FAQ", Toast.LENGTH_SHORT).show();
        }
        return true;
    }
}

2 个答案:

答案 0 :(得分:3)

您应该添加 .show() 。阅读Toast

  

Toast.makeText(context,text,duration).show();

if (uName.getText().toString().equals("admin") && pass.getText().toString().equals("pass")) {
    Intent myIntent = new Intent(MainPage.this, TasksPanel.class);
    startActivity(myIntent);
    finish();
} else {
    Toast.makeText(getApplicationContext(), "Incorrect Username or Password", Toast.LENGTH_SHORT).show();
}

答案 1 :(得分:0)

@Override
public void onClick(View v)
 {          
    if(uName.getText().toString().equals("admin") && pass.getText().toString().equals("pass"))
    {
     Intent myIntent = new Intent(MainPage.this, TasksPanel.class);
     startActivity(myIntent);
     finish();
    }
    else
    {  
     Toast.makeText(getApplicationContext(),"Incorrect Username or Password",Toast.LENGTH_SHORT).show();
    }
 }