简体   繁体   English

从按钮上的assets文件夹中打开PDF,按标签片段

[英]Opening a PDF from the assets folder on a button press in a tab fragment

I have an Android app with 3 tabs. 我有一个带有3个标签的Android应用。 I want to put a CardView that when clicked opens a PDF from the assets folder (through intents). 我想放置一个CardView,单击该CardView可以从资产文件夹(通过意图)打开PDF。

Right now I have a button in one of the tabs that shows a toast when tapped, so I know it works. 现在,我在其中一个选项卡中有一个按钮,当点击它时会显示一个吐司,所以我知道它可以工作。 How do I go about making it so that tapping the button opens a PDF from the assets folder instead? 我如何才能做到这一点,以便轻点按钮即可从资产文件夹中打开PDF?

I've read a ton of answers on stackoverflow but none of them seemed to work for me. 我已经阅读了很多关于stackoverflow的答案,但是似乎没有一个对我有用。 I think it might be because I have a MainActivity.java file, and 3 other TabFragment.java files, and there's something I'm not doing right. 我认为这可能是因为我有一个MainActivity.java文件和其他3个TabFragment.java文件,并且有些事情我做得不对。 Or maybe it's something completely different, I don't know. 也许这是完全不同的东西,我不知道。

Here is my MainActivity.java code: 这是我的MainActivity.java代码:

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

private SectionsPageAdapter mSectionsPageAdapter;

private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d(TAG, "onCreate: Starting.");

    mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    setupViewPager(mViewPager);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
}

private void setupViewPager(ViewPager viewPager) {
    SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
    adapter.addFragment(new Tab1Fragment(), "TAB1");
    adapter.addFragment(new Tab2Fragment(), "TAB2");
    adapter.addFragment(new Tab3Fragment(), "TAB3");
    viewPager.setAdapter(adapter);
}}

Tab1Fragment.java code is: Tab1Fragment.java代码是:

public class Tab1Fragment extends Fragment {


private static final String TAG = "Tab1Fragment";

private Button btnTEST;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tab1_fragment, container, false);
    btnTEST = (Button) view.findViewById(R.id.btnTEST);

    btnTEST.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "TESTING BUTTON CLICK 1", Toast.LENGTH_SHORT).show();




        }
    });

    return view;

}}

and tab1_fragment.xml is 和tab1_fragment.xml是

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"></LinearLayout>

    <android.support.v7.widget.CardView
        android:id="@+id/cardView1"
        android:layout_width="fill_parent"
        android:layout_height="75dp"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:onClick="cardView1"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="10dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="25sp"
        android:text="Tab1"
        android:layout_marginTop="40dp"
        android:id="@+id/textTab1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnTEST"
        android:text="TESTBTN 1"/>
</RelativeLayout>

Any help would be appreciated. 任何帮助,将不胜感激。

In your Button click event put this code. 在您的Button click事件中,输入以下代码。

btnTEST.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        Toast.makeText(getActivity(), "TESTING BUTTON CLICK 1", Toast.LENGTH_SHORT).show();

        File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+"/pdfname.pdf");
        if (file.exists())
        {
            Intent intent=new Intent(Intent.ACTION_VIEW); 
            Uri uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        try
        {
            startActivity(intent);
        }
        catch(ActivityNotFoundException e)
        {
            Toast.makeText(getActivity(), "No Application available to view pdf", Toast.LENGTH_LONG).show(); 
        }
      }


   }
});

Hope, It will help you... 希望,它将帮助您...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM