2010-06-29 7 views
0

나는 로그인 대화 상자를 사용합니다. 따라서이 대화 상자를 닫으면 show() 대화 상자에 지정된 값이 모두 손실됩니다. 이 가치를 되 찾는 방법? 내 코드는 다음과 같습니다경고창을 닫은 후 값을 다시 가져오고 싶습니다.

private void accessPinCode() 
{ 
    LayoutInflater factory = LayoutInflater.from(this); 
    final View textEntryView = factory.inflate(R.layout.dialog_login, null); 
    AlertDialog.Builder alert = new AlertDialog.Builder(this);     
    alert.setTitle("Title"); 
    alert.setMessage("Enter Pin :");     
    alert.setView(textEntryView);  

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) {   
      EditText mUserText; 
      mUserText = (EditText) textEntryView.findViewById(R.id.txt_password); 
      //String strPinCode = mUserText.getText().toString(); 
      Log.d(TAG, "Pin Value 1 : " + mUserText.getText().toString());    
      strPIN = mUserText.getText().toString(); 
      Log.d(TAG, "strPIN inside accessPinCode : " + strPIN); 
      fPIN= checkPINCode(); 
      Log.d(TAG, "fPass : " + fPIN); 


      return;     
     } 
    }); 

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      return; 
     } 
    }); 

    alert.show(); 
    Log.d(TAG, "strPIN outside Alert Show : " + strPIN); 
} 

내 코드를 기반으로 strPIN 및 FPIN 값이 손실됩니다. 나는 accessPinCode 함수 밖에서 그 값을 사용하고 싶다. 얻는 방법?

실제로이 함수는 tabchanged 이벤트에서 호출됩니다. 로그인 패스가되면 사용자는 다른 탭에 액세스 할 수 있습니다. AlertDialog의 Ok 버튼을 클릭하기 전에 이미 탭 변경 이벤트에서 작업했습니다. 내 탭 이벤트는 아래와 같습니다.

tabHost.setOnTabChangedListener(new OnTabChangeListener() { 

      public void onTabChanged(String tabId) { 
       // TODO Auto-generated method stub 

       if (tabId.equals("index")) 
       { 
        tabHost.setCurrentTab(1); 
        accessPinCode(); 
       } 
       Log.d(TAG, "tabId : "+ tabId);  
      } 
     }); 

로그인에 적합한 대화 상자 유형이 있습니까? 어떻게 해결할 수 있습니까?

+0

작은 충고 : "ok"와 "Cancel"대신에'android.R.string.ok'과'android.R.string.cancel'을 사용하십시오. – Felix

+0

감사합니다. I will – soclose

+0

마지막으로, Tab OnChanged 이벤트 내에서 accessPinCode를 호출하는 대신이 이벤트 내에 모든 alertDialog 만들기를 넣습니다. 로그인 패스가 있으면 현재 탭을 설정하고 싶습니다. 다들 감사 해요. – soclose

답변

1

편집 : 네거티브/포지티브 버튼에서 주변 클래스의 함수를 호출하고이 두 매개 변수를 AlertDialog에 수집 된 값으로 설정해야합니다.

뭔가 같은 :

private String mStrPin; 
private float mFPin; 

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

...

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
      String strPin = "1234"; 
      float fPin = 1.234f; 
      public void onClick(DialogInterface dialog, int which) { 
       loggedIn(strPin, fPin); 
      } 
    } 

...

} 
private void loggedIn(String strPin, float fPin) { 
    mStrPin = strPin; 
    mFPin = fPin; 
} 
+0

첫 번째 부분은 +1, 마지막 부분은 -1입니다 (심각하게 권장합니까?). – Felix

0

단순화 된 예 :

public interface TextListener { 
    void onPositiveResult(CharSequence text); 
} 

public static AlertDialog getTextDialog(Context ctx, 
     final TextListener listener) { 
    View view = LayoutInflater.from(ctx).inflate(R.layout.dialog, null); 
    final TextView tv = (TextView) view.findViewById(R.id.tv); 
    AlertDialog.Builder builder = new AlertDialog.Builder(ctx); 
    builder.setView(view); 
    // 
    builder.setPositiveButton(android.R.string.ok, new OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      listener.onPositiveResult(tv.getText()); 
     } 
    }); 
    builder.setNegativeButton(android.R.string.cancel, null); 
    return builder.create(); 
} 
+0

@ alex, pls 내 게시물을 다시 확인하십시오. 나는 다시 편집한다. – soclose