2016-06-15 9 views
0

내 "LoginBtn"에 대한 내 onclicklistener입니다. 그러나 그 블록 내의 코드는 한 번만 실행되고 다시 실행되지 않습니다. 제발 도와주세요, 나는 모든 것을 시도했습니다. 로그를 실행했기 때문에 값을 한 번 로그 한 다음 다시는 로그 아웃하지 않기 때문에 알 수 있습니다.Onclicklistener는 한 번만 작동합니다.

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", "[email protected]"); 
        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 
+0

Loginbtn = (버튼) findViewById (R.id.loginbtn); Loginbtn.setOnClickListener (this); – revipod

+0

당신의 logcat을 붙이십시오. –

+0

당신은 디버거를 사용할 수 있습니다. 클릭이 작동합니다. 당신은 asynctask를 호출해서는 안된다. 더 이상 비동기가 아니다. – Raghunandan

답변

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", "[email protected]"); 
        params.put("password", "blabla"); 
        LoginRequest(params); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
+0

여전히 똑같은 것이다. 로그캣은 -15 16 : 15 : 44.048 2377-2377/com.example.jj.test를 보여줍니다. D/TEST : loginform은 false입니다. – revipod

+0

ok를 여러 번 눌러도 기본적으로 변수 loginform을 초기화 한 다음 값을 얻으면 변수 "loginform"을 정적으로 확인하거나 설정합니다. –

관련 문제