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 );    
            }
        });

ログインに適したダイアログの種類はありますか? の解き方?

4

2 に答える 2

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;
}
于 2010-06-29T09:23:16.880 に答える
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();
}
于 2010-06-29T11:39:44.183 に答える