将我现有的java类转换为片段java类

时间:2017-05-19 16:47:25

标签: android

所以我想将以下代码从普通的java类移动到片段对象。

到目前为止:

正常活动`

公共类ChartsActivity扩展了AppCompatActivity {

private MaterialSearchBar searchview;
private EditText pdf;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_charts);
    searchview = (MaterialSearchBar) findViewById(R.id.edit_text);
    final ProgressBar Pbar;
    Pbar = (ProgressBar) findViewById(R.id.progressBar2);

    pdf = (EditText) findViewById(R.id.pdf);


    final Button button = (Button) findViewById(R.id.get_edit_view_button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            WebView myWebView = (WebView) findViewById(R.id.webview);
            myWebView.loadUrl("http://www.xyzchartsxyz.esy.es/files/android/virtualhub/charts/overviews/" +searchview.getText() +pdf.getText() );
            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);

            myWebView.getSettings().setSupportZoom(true);
            myWebView.getSettings().setBuiltInZoomControls(true);
            myWebView.setBackgroundColor(Color.TRANSPARENT);

            myWebView.setWebChromeClient(new WebChromeClient());{

            }

            myWebView.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) {
                    if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){
                        Pbar.setVisibility(ProgressBar.VISIBLE);
                    }

                    Pbar.setProgress(progress);
                    if(progress == 100) {
                        Pbar.setVisibility(ProgressBar.GONE);

                    }
                }
            });
        }


    });

}

}

&#39;

这是我到目前为止的片段..

public class OneFragment extends Fragment{

private MaterialSearchBar searchview;
private EditText pdf;

public OneFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.activity_charts, container, false);

}

}

1 个答案:

答案 0 :(得分:1)

更改片段的onView createView方法,以便在您的活动中实现相同的目标

 View view = inflater.inflate(R.layout.activity_charts, container, false);
    searchview = (MaterialSearchBar)view.findViewById(R.id.edit_text);
    final ProgressBar Pbar;
    Pbar = (ProgressBar)view.findViewById(R.id.progressBar2);

    pdf = (EditText)view.findViewById(R.id.pdf);


    final Button button = (Button)view. findViewById(R.id.get_edit_view_button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            WebView myWebView = (WebView)biew.findViewById(R.id.webview);
            myWebView.loadUrl("http://www.xyzchartsxyz.esy.es/files/android/virtualhub/charts/overviews/" +searchview.getText() +pdf.getText() );
            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);

            myWebView.getSettings().setSupportZoom(true);
            myWebView.getSettings().setBuiltInZoomControls(true);
            myWebView.setBackgroundColor(Color.TRANSPARENT);

            myWebView.setWebChromeClient(new WebChromeClient());{

            }

            myWebView.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) {
                    if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){
                        Pbar.setVisibility(ProgressBar.VISIBLE);
                    }

                    Pbar.setProgress(progress);
                    if(progress == 100) {
                        Pbar.setVisibility(ProgressBar.GONE);

                    }
                }
            });
        }


    });
return view;
}