1. 程式人生 > >內部類呼叫外部類方法時傳參的問題

內部類呼叫外部類方法時傳參的問題

上問題程式碼

public class SettingActivity extends Activity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initRemoteLockScreen();
    }

public void initRemoteLockScreen(){
        SettingClickView rls = (SettingClickView) findViewById(R.id.siv_remoteLockScreen);
        rls.setTitle("遠端鎖屏密碼設定"
); rls.setDesc("點選進行設定密碼"); rls.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { startremotelockdialog(); } }); } public void startremotelockdialog(){ showPasswordSetDailog(dialogTitle,new
AlertDialogEngineInterface() { @Override public void saveSp(String value) { if (value == null){ return; } mPref.edit() .putString("lockScreenPass", MD5Utils.encode(value)).commit(); } }); } public
void showPasswordSetDailog(String title, final AlertDialogEngineInterface dialogInterface) { AlertDialog.Builder builder = new AlertDialog.Builder(this); final AlertDialog dialog = builder.create(); View view = View.inflate(this, R.layout.dailog_set_password, null); dialog.setView(view, 0, 0, 0, 0); TextView tvTitle = (TextView) findViewById(R.id.dialog_tv_title); tvTitle.setText(title); Log.e("xxx",title);//**title值不為空** final EditText etPassword = (EditText) view .findViewById(R.id.et_password); final EditText etPasswordConfirm = (EditText) view .findViewById(R.id.et_password_confirm); Button btnOK = (Button) view.findViewById(R.id.btn_ok); Button btnCancel = (Button) view.findViewById(R.id.btn_cancel); btnOK.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String password = etPassword.getText().toString(); String passwordConfirm = etPasswordConfirm.getText().toString(); if (!TextUtils.isEmpty(password) && !passwordConfirm.isEmpty()) { if (password.equals(passwordConfirm)) { dialogInterface.saveSp(password); dialog.dismiss(); } } }); btnCancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss();// 隱藏dialog } }); dialog.show(); } private interface AlertDialogEngineInterface{ void saveSp(String value); } }

結論:
內部類呼叫外部類方法時傳參是可以傳參的,不管外部類方法是private,還是public;而且內部類裡可以修改外部類的成員變數。
如果外部類方法無法得到內部類區域性變數的值,那應該是哪裡寫錯了。