意图按钮在第二个活动中没有响应

时间:2014-12-03 15:26:28

标签: android android-intent

login.java
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class login extends Activity implements OnClickListener {
// deklarasi variable
Button button1;
AlertDialog alert;
EditText password;
EditText user;
String isi_passwd, username;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);


 // inisialisasi variabel
        button1 = (Button) this.findViewById(R.id.button1);
        button1.setOnClickListener(this);

        // edit text untuk username
        user = (EditText) findViewById(R.id.username);
        user.getText();

        // edit text untuk password
        password = (EditText) findViewById(R.id.editText2);
        password.getText();


        username="user";
        isi_passwd = "1234";



    }

    @Override
    // method untuk override tombol button1
    public void onClick(View tombol_act) {
        // TODO Auto-generated method stub
        if (tombol_act == button1) {

            if (password.getText().toString().equals(isi_passwd) && user.getText().toString().equals(username)) {
                AlertDialog.Builder pesan = new AlertDialog.Builder(this);
                pesan.setMessage("Nama  dan No NIK anda benar")
                        .setCancelable(false).setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        // TODO Auto-generated method stub
                                        setContentView(R.layout.index);
                                    }
                                });
                alert = pesan.create();
                alert.show();
            } else {
                AlertDialog.Builder pesan = new AlertDialog.Builder(this);
                pesan.setMessage("Nama :" + user.getText() + " dan No NIK Anda :" + password.getText() + " anda masih SALAH")
                        .setCancelable(false).setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        // TODO Auto-generated method stub
                                        user.setText("");
                                        password.setText("");
                                    }
                                });
                alert = pesan.create();
                alert.show();
            }
        }

    }
 } 
index.java or (Here intent button does not respond after login with java on top)
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;

public class home extends Activity {

     @Override
     protected void onCreate(Bundle saveInstantState) {
      super.onCreate(saveInstantState);
      setContentView(R.layout.index);
        Button button1=(Button)findViewById(R.id.btn1);




        button1.setOnClickListener(new View.OnClickListener() {



            public void onClick(View arg0) {

                // TODO Auto-generated method stub

                Intent i =new Intent(getApplicationContext(),isiform.class);

                startActivity(i);



            }

        });



     }
}



manifest.xml
    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.kelurahan.tanahbaru.login"

            android:label="@string/app_name" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>


            <activity android:name=".home"></activity>
            <activity android:name=".isiform"></activity>
       <activity android:name=".lihatform"></activity> 
         <activity android:name=".feedback"></activity> 

    </application>

</manifest>

当我通过在manifest主login.java中设置主要活动来登录以填充登录页面中的用户名和密码 我无法抑制或意图按钮在Activity home.java中没有响应 我真的很头晕为什么按钮在活动中没有活动到两个或在home.java请帮帮我

1 个答案:

答案 0 :(得分:0)

我们不会在onClick上编写代码,你不能简单地比较引用:

tombol_act == button1  //that's wrong.

它类似于:

public void onClick(View view) {

switch(view.getId())
{
    case R.id.your_view1:
        // the code goes here
    break;

    case R.id.your_view2:
        // the code goes here
    break;
}

}