按钮无响应

时间:2015-01-21 18:10:36

标签: android eclipse sqlite button login

我认为有两个相当基本的问题需要回答:

1)当我在主屏幕上运行我的模拟器时,我的SignIn按钮没有响应,我不确定为什么我尝试了其他方法,但每当我点击没有任何反应并且没有显示错误。代码如下所示:

package com.techblogon.loginexample;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class HomeActivity extends Activity 
{
    Button btnSignIn,btnSignUp;
    LoginDataBaseAdapter loginDataBaseAdapter;

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

         // create a instance of SQLite Database
         loginDataBaseAdapter=new LoginDataBaseAdapter(this);
         loginDataBaseAdapter=loginDataBaseAdapter.open();

         // Get The Reference Of Buttons
         btnSignIn=(Button)findViewById(R.id.buttonSignIN);
         btnSignUp=(Button)findViewById(R.id.buttonSignUP);

        // Set OnClick Listener on SignUp button 
        btnSignUp.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub

            /// Create Intent for SignUpActivity  and Start The Activity
            Intent intentSignUP=new Intent(getApplicationContext(),SignUPActivity.class);
            startActivity(intentSignUP);
            }
        });
    }
    // Method to handleClick Event of Sign In Button
    public void signIn(View V)
       {
            final Dialog dialog = new Dialog(HomeActivity.this);
            dialog.setContentView(R.layout.login);
            dialog.setTitle("Login");


            final  EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
            final  EditText editTextPassword=(EditText)dialog.findViewById(R.id.editTextPasswordToLogin);

            Button btnSignIn=(Button)dialog.findViewById(R.id.buttonSignIN);

            // Set On ClickListener
            btnSignIn.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // get The User name and Password
                    String userName=editTextUserName.getText().toString();
                    String password=editTextPassword.getText().toString();

                    // fetch the Password form database for respective user name
                    String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);

                    // check if the Stored password matches with  Password entered by user
                    if(password.equals(storedPassword))
                    {
                        Toast.makeText(HomeActivity.this, "Welcome", Toast.LENGTH_LONG).show();
                        dialog.dismiss();

                        Intent ii=new Intent(HomeActivity.this,MainMenu.class);
                        startActivity(ii);
                    }
                    else
                    {
                        Toast.makeText(HomeActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
                    }
                }
            });

            dialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Close The Database
        loginDataBaseAdapter.close();
    }
}

有人可以为我提供最好的sqlite数据库查看器,我希望查看我在模拟器上创建的数据库的记录

我的XML如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/picture" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Hello, Welcome"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/buttonSignIN"
        android:layout_width="fill_parent"
        android:layout_height="70dp"
        android:text="Sign In" />

    <Button
        android:id="@+id/buttonSignUP"
        android:layout_width="fill_parent"
        android:layout_height="70dp"
        android:text="Sign Up" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您错过了buttonSignIN按钮

的xml中的属性
android:onClick="signIn"

试试这个,

        <Button
        android:id="@+id/buttonSignIN"
        android:layout_width="fill_parent"
        android:layout_height="70dp"
        android:onClick="signIn"
        android:text="Sign In" />

您需要了解在java代码中设置clickListener和从按钮单击设置xml属性之间的区别。这两种方式可以实现任何元素的点击事件。

相关问题