Java 在选项卡片段中按按钮从资产文件夹打开PDF
我有一个Android应用程序,有3个标签。我想放置一个CardView,当单击它时,会从资产文件夹(通过意图)打开一个PDF 现在,我在其中一个标签上有一个按钮,当点击时会显示一个祝酒词,所以我知道它是有效的。我该如何做才能让点击按钮打开资产文件夹中的PDF文件 我已经读了很多关于stackoverflow的答案,但似乎没有一个适合我。我想这可能是因为我有一个MainActivity.java文件和另外3个TabFragment.java文件,而有些事情我做得不对。或者是完全不同的东西,我不知道 以下是我的MainActivity.java代码:Java 在选项卡片段中按按钮从资产文件夹打开PDF,java,android,android-studio,android-fragments,Java,Android,Android Studio,Android Fragments,我有一个Android应用程序,有3个标签。我想放置一个CardView,当单击它时,会从资产文件夹(通过意图)打开一个PDF 现在,我在其中一个标签上有一个按钮,当点击时会显示一个祝酒词,所以我知道它是有效的。我该如何做才能让点击按钮打开资产文件夹中的PDF文件 我已经读了很多关于stackoverflow的答案,但似乎没有一个适合我。我想这可能是因为我有一个MainActivity.java文件和另外3个TabFragment.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>
任何帮助都将不胜感激。在您的按钮单击事件中输入此代码
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();
}
}
}
});
希望,这将帮助您……我在我的toast消息行后添加了您的代码(我将其注释掉以检查代码中的toast是否会显示)。代码中的toast消息会在按下按钮时显示,但不幸的是,它不会执行任何其他操作。我回忆起这里关于从资产文件夹读取文件的大部分答案,提到
AssetManager
和getAssets()代码>,所以我认为这也是需要的。它是否工作???。如果不工作,请告诉我问题出在哪里。在键入整个内容之前,不小心按了enter键,无论如何,我在我的toast消息行后添加了您的代码(我对其进行了注释,以检查代码中的toast是否会显示)。代码中的toast消息会在按下按钮时显示,但不幸的是,它不会执行任何其他操作。我回忆起这里关于从资产文件夹读取文件的大部分答案,提到AssetManager
和getAssets()代码>所以我认为在某个地方也需要这样做。