将活动转换为片段

时间:2014-06-22 14:14:43

标签: android pdf android-fragments

我想将我的活动转换为片段可以任何一个帮助将活动转换为Fragment..i已经创建了Pdf
阅读器(单击按钮它将使用pdfviwer.jar库在另一个激活中打开Pdf文件...我想要当我单击按钮Pdf在右侧的同一屏幕上打开时) 我需要帮助请解决我的问题

我发布我的活动

maiinActivity.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btn =(Button)findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub



    AssetManager assetManager = getAssets();

    InputStream in = null;
    OutputStream out = null;
    File file = new File(getFilesDir(), "ABC.pdf");
    try
    {
        in = assetManager.open("ABC.pdf");
        out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

        copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e)
    {
        Log.e("tag", e.getMessage());
    }

    Intent intent = new Intent(getApplicationContext(), MyPdfViewerActivity.class);
    intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, getFilesDir()+"/ABC.pdf");
    startActivity(intent);
    /* 
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(
            Uri.parse("file://" + getFilesDir() + "/ABC.pdf"),
            "application/pdf");

    startActivity(intent);*/
}
 });

}

private void copyFile(InputStream in, OutputStream out) throws IOException
{
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1)
    {
        out.write(buffer, 0, read);
    }
}
  }

PdfviwerActivty.java

public class MyPdfViewerActivity extends PdfViewerActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // TODO Auto-generated method stub
}

public int getPreviousPageImageResource() { return R.drawable.left_arrow; }
public int getNextPageImageResource() { return R.drawable.right_arrow; }
public int getZoomInImageResource() { return R.drawable.zoom_in; }
public int getZoomOutImageResource() { return R.drawable.zoom_out; }
public int getPdfPasswordLayoutResource() { return R.layout.pdf_file_password; }
public int getPdfPageNumberResource() { return R.layout.dialog_pagenumber; }
public int getPdfPasswordEditField() { return R.id.etPassword; }
public int getPdfPasswordOkButton() { return R.id.btOK; }
public int getPdfPasswordExitButton() { return R.id.btExit; }
public int getPdfPageNumberEditField() { return R.id.pagenum_edit; }

}

2 个答案:

答案 0 :(得分:0)

尝试“extends FragmentActivity”而不是“extends Activity”..

答案 1 :(得分:0)

您应该使用FragementActivity而不是Activity,并将您的ui组件粘贴到片段的onCreateView() - Method中。有关详细信息,请参阅本教程:http://www.vogella.com/tutorials/AndroidFragments/article.html