繁体   English   中英

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

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

我有一个带有3个标签的Android应用。 我想放置一个CardView,单击该CardView可以从资产文件夹(通过意图)打开PDF。

现在,我在其中一个选项卡中有一个按钮,当点击它时会显示一个吐司,所以我知道它可以工作。 我如何才能做到这一点,以便轻点按钮即可从资产文件夹中打开PDF?

我已经阅读了很多关于stackoverflow的答案,但是似乎没有一个对我有用。 我认为这可能是因为我有一个MainActivity.java文件和其他3个TabFragment.java文件,并且有些事情我做得不对。 也许这是完全不同的东西,我不知道。

这是我的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代码是:

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;

}}

和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>

任何帮助,将不胜感激。

在您的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(); 
        }
      }


   }
});

希望,它将帮助您...

暂无
暂无

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

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