2010-06-29 74 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的确定按钮之前,全部已经在制表符更改的事件中工作。我的标签事件如下

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

小建议:使用'android.R.string.ok'和'android.R.string.cancel'来代替“Ok”和“Cancel”。 – Felix 2010-06-29 10:44:24

+0

谢谢。我将 – soclose 2010-06-29 13:29:49

+0

最后,我没有在Tab OnChanged事件中调用accessPinCode,而是将所有的alertDialog创建在这个事件中。因为如果登录通过,我想设置当前选项卡。谢谢你们。 – soclose 2010-06-30 03:00:56

回答

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 2010-06-29 10:43:35

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,请重新检查我的帖子。我再次编辑。 – soclose 2010-06-30 02:00:57