我有一个包含3个标签的Android应用。我想在单击时放置一个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>
任何帮助都将不胜感激。
答案 0 :(得分:0)
在按钮点击事件中输入此代码。
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();
}
}
}
});
希望,它会帮助你...