2016-06-15 2 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

まだ同じことです。 Logcatは-15 16:15:44.048 2377-2377/com.example.jj.testのみを表示します。D/TEST:loginformはfalseです。 – revipod

+0

okを複数回押しても、デフォルトで変数loginformを初期化してから、値を取得したら変数 "loginform"を静的にチェックしてください。 –

関連する問題