Onclicklistener仅工作一次

时间:2016-06-15 10:35:54

标签: java android button onclicklistener

我的onclicklistener为我的“LoginBtn”。但是,该块内的代码只运行一次,然后再运行一次。请帮助我,我已经尝试了一切。我知道,因为我已经运行了日志,它只记录了一次值,然后再也没有。

package com.example.jj.test;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ScaleDrawable;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatDrawableManager;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.goebl.david.Webb;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity implements   View.OnClickListener{


    private static final String TAG = "test";
    boolean loginform;
    Button Loginbtn;
    ImageView logoIV;
    String email;
    String password;
    String token;
    EditText emailET;
    EditText passwordET;
    final Webb webb = Webb.create();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Loginbtn = (Button) findViewById(R.id.loginbtn);
        Loginbtn.setOnClickListener(this);
        logoIV = (ImageView) findViewById(R.id.logoIV);
        emailET = (EditText) findViewById(R.id.emailET);
        passwordET = (EditText) findViewById(R.id.passwordET);
        loginform = false;
        ModifyEditText();

    }


    public void ModifyEditText(){
        Drawable drawable = getResources().getDrawable(R.mipmap.email);
        drawable.setBounds(0, 0, (int) (drawable.getIntrinsicWidth() * 0.6),
                (int) (drawable.getIntrinsicHeight() * 0.6));
        ScaleDrawable sd = new ScaleDrawable(drawable, 0, 40, 40);
        emailET.setCompoundDrawables(null, null,sd.getDrawable(), null);
        drawable = getResources().getDrawable(R.mipmap.password);
        drawable.setBounds(0, 0, (int) (drawable.getIntrinsicWidth() * 0.6),
                (int) (drawable.getIntrinsicHeight() * 0.6));
        sd = new ScaleDrawable(drawable, 0, 40, 40);
        passwordET.setCompoundDrawables(null, null, sd.getDrawable(), null);
        final Drawable d = emailET.getBackground();
        final Drawable nd = d.getConstantState().newDrawable();
        nd.setColorFilter(AppCompatDrawableManager.getPorterDuffColorFilter(
                Color.parseColor("#FFFFFF"), PorterDuff.Mode.SRC_IN));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            emailET.setBackground(nd);
            passwordET.setBackground(nd);
        }
    }
    public void startAnimation() {
        Loginbtn.setText("Login");
        logoIV.startAnimation(AnimationUtils.loadAnimation(this,  R.anim.animmovetop));
        Loginbtn.startAnimation(AnimationUtils.loadAnimation(this, R.anim.animmovedown));
        Animation animFadeIn = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
        emailET.setAnimation(animFadeIn);
        passwordET.setAnimation(animFadeIn);
        emailET.setVisibility(View.VISIBLE);
        passwordET.setVisibility(View.VISIBLE);
        Loginbtn.setBackgroundResource(R.drawable.transparentrectangel);
        loginform = true;
    }


    public void LoginRequest(final JSONObject id) throws Exception {

        final String testurl = "http://api.dermatrax.com/api/v1/token/generate?email="+email+"&password="+password+;
        new AsyncTask<Void, Void, JSONObject>() {
            @Override
            protected JSONObject doInBackground(Void... params) {
                try {
                    Log.d("TEST","Sending request");
                    JSONObject response = webb
                            .post(testurl)
                            .body(id)
                            .ensureSuccess()
                            .readTimeout(4000)
                            .asJsonObject()
                            .getBody();
                    return response;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
            @Override
            protected void onPostExecute(JSONObject result) {
                if (result != null) {
                    Log.d("TEST", result.toString());
                    try {
                        JSONObject data = result.getJSONObject("data");
                        token = data.get("token").toString();
                        Log.d("TEST", token);
                        if (token != null && !token.isEmpty()) {
                            Intent intent = new Intent(getApplicationContext(), UserData.class);
                            intent.putExtra("token", token);
                            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                            startActivity(intent);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                else{
                    Toast.makeText(getApplicationContext(),"Wrong email or password",Toast.LENGTH_LONG);
                }
            }
        }.execute().get();
        //executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
    }


    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.loginbtn){
            Log.d("TEST", "loginform is = " + loginform);
            JSONObject params = new JSONObject();
            if (loginform == false) {
                try {
                    startAnimation();
                    //LoginRequest(params);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (loginform == true) {

                email = emailET.getText().toString();
                password = passwordET.getText().toString();
                try {
                    //params.put("email", email);
                    //params.put("password", password);
                    params.put("email", "blabla@combustiongroup.com");
                    params.put("password", "blabla");
                    LoginRequest(params);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

xml代码

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context="com.example.jj.test.MainActivity">


    <RelativeLayout
        android:background="#292446"
        android:orientation="vertical"
        android:id="@+id/midLL"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <Button
            android:padding="10dp"
            android:layout_width="300dp"
            android:gravity="center"
            android:layout_height="40dp"
            android:text="Go"
            android:id="@+id/loginbtn"
            android:textColor="#FFFFFF"
            android:background="@drawable/roundbutton"
            android:layout_below="@+id/logoIV"
            android:layout_centerHorizontal="true" />

        <de.hdodenhof.circleimageview.CircleImageView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/logoIV"
            android:src="@mipmap/bigmatchlogo"
            android:layout_width="70dp"
            android:layout_height="70dp"
            app:civ_border_width="2dp"
            app:civ_border_color="#d6d6d6"
            android:layout_alignBottom="@+id/emailET"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="20dp" />


        <EditText
            android:layout_marginBottom="10dp"
            android:textColorHint="#FFFFFF"
            android:hint="Email"
            android:visibility="invisible"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:id="@+id/emailET"
            android:layout_marginTop="217dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />

        <EditText
            android:drawableRight="@mipmap/password"
            android:hint="Password"
            android:textColorHint="#FFFFFF"
            android:visibility="invisible"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:inputType="textPassword"
            android:ems="10"
            android:layout_marginTop="10dp"
            android:id="@+id/passwordET"
            android:layout_below="@+id/emailET"
            android:layout_alignLeft="@+id/emailET"
            android:layout_alignStart="@+id/emailET" />
    </RelativeLayout>



    </LinearLayout>

记录猫

    06-15 15:49:11.898 8631-8631/com.example.jj.test D/TEST: loginform is = false
06-15 15:50:36.598 8631-8631/com.example.jj.test D/szxszxszxszxszx: spannableStringBuilder.......1
06-15 15:50:36.598 8631-8631/com.example.jj.test D/szxszxszx: setSpan start is 0,end is 0,flags is 18boolena is false

1 个答案:

答案 0 :(得分:0)

将此代码写入onCreate()...

Loginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    if(v.getId() == R.id.loginbtn){
            Log.d("TEST", "loginform is = " + loginform);
            JSONObject params = new JSONObject();
            if (loginform == false) {
                try {
                    startAnimation();
                    //LoginRequest(params);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (loginform == true) {

                email = emailET.getText().toString();
                password = passwordET.getText().toString();
                try {
                    //params.put("email", email);
                    //params.put("password", password);
                    params.put("email", "blabla@combustiongroup.com");
                    params.put("password", "blabla");
                    LoginRequest(params);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }