如何访问片段中oncreateView()之外的视图

时间:2016-02-16 12:23:08

标签: java android

在片段中,我有一个进度条。我想隐藏oncreateView()方法之外的进度条。我尝试了一些代码但不隐藏

请任何人修复。

这是我的代码:

public class ChangeMobileOtp extends Fragment {

    static TextView mobcode;
    Button verify;
    ProgressBar prgSpin;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.change_mobile_code, container,
                false);

        mobcode = (TextView)view.findViewById(R.id.mobile_verificationtxt);
        verify = (Button)view.findViewById(R.id.mverify_button);
        prgSpin = (ProgressBar)view.findViewById(R.id.mobSpin);
        prgSpin.setVisibility(view.VISIBLE);

        return view;

    }


    public void recivedSms(String message)
    {
        try
        {

            mobcode.setText(message);
            Log.d("otps", message);
            prgSpin.setVisibility(View.GONE); // hide progress bar

        }
        catch (Exception e)
        {
        }
    }
}

另一个javaClass

public class IncomingOtp extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {

        final Bundle bundle = intent.getExtras();
        try {
            if (bundle != null)
            {
                final Object[] pdusObj = (Object[]) bundle.get("pdus");
                for (int i = 0; i < pdusObj .length; i++)
                {
                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[])                                                                                                    pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                    String senderNum = phoneNumber ;
                    String message = currentMessage .getDisplayMessageBody();
                    String otp = message.replaceAll("[^0-9]", "");
                    try
                    {
                        if (senderNum.contains("RIDSHR"))
                        {
                            ChangeMobileOtp Sms = new ChangeMobileOtp();
                            Sms.recivedSms(otp.trim().substring(1));
                            Log.d("Otp",otp.trim().substring(1));
                        }
                    }
                    catch(Exception e){}

                }
            }

        } catch (Exception e)
        {

        }
    }

}

请任何人帮忙解决这个问题。

提前致谢!

1 个答案:

答案 0 :(得分:0)

首先为进度条创建一个类似于下面的功能类:

ProgressUtil.java:

    public class Book {
      public String book_id;
      public String book_title;
      public String book_cost;
   }

}

现在在你的片段中,在类中创建一个progressDialog变量:

public class ProgressUtil {


public static void showProgressDialog(ProgressDialog pDialog) {
    pDialog.setMessage("Loading...");
    pDialog.setCancelable(false);
    if (!pDialog.isShowing())
        pDialog.show();
}

public static void hideProgressDialog(ProgressDialog pDialog) {
    if (pDialog.isShowing())
        pDialog.dismiss();
}

在onCreateView()方法中初始化:

 ProgressDialog progressDialog;

现在,无论您想要显示或隐藏进度条,请根据您的要求使用以下命令:

 progressDialog=new ProgressDialog(getActivity());

希望它会有所帮助。