Android Java页面开发需求:输入数字点击按钮跳转对应PDF链接
Solution for Dynamic PDF Redirection Based on User Input
Here's a step-by-step implementation to meet your requirements:
1. Layout File (res/layout/activity_main.xml)
First, create the UI with an EditText for number input and a Button to trigger the PDF redirect:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp" android:gravity="center"> <EditText android:id="@+id/input_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="Enter 1 or 2" android:inputType="number" android:textSize="18sp"/> <Button android:id="@+id/open_pdf_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Open PDF" android:layout_marginTop="20dp"/> </LinearLayout>
2. Activity Logic (MainActivity.java)
Implement the click listener to validate input and launch the appropriate PDF link:
import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private EditText inputNumber; private Button openPdfBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); inputNumber = findViewById(R.id.input_number); openPdfBtn = findViewById(R.id.open_pdf_btn); openPdfBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String input = inputNumber.getText().toString().trim(); // Validate input if (input.isEmpty()) { Toast.makeText(MainActivity.this, "Please enter a number", Toast.LENGTH_SHORT).show(); return; } String pdfUrl; switch (input) { case "1": pdfUrl = "https://www.mywebsite/myfile1.pdf"; break; case "2": pdfUrl = "https://www.mywebsite/myfile2.pdf"; break; default: Toast.makeText(MainActivity.this, "Please enter 1 or 2", Toast.LENGTH_SHORT).show(); return; } // Create intent to open PDF Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse(pdfUrl), "application/pdf"); // Check if there's an app to handle the intent if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Toast.makeText(MainActivity.this, "No PDF viewer app found", Toast.LENGTH_SHORT).show(); } } }); } }
3. Update AndroidManifest.xml
Add the INTERNET permission since we're accessing online PDFs, and ensure your activity is registered:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yourpackage.name"> <!-- Required for accessing online PDFs --> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
Key Notes:
- Input Validation: The code checks for empty input and valid numbers (1 or 2), showing toast messages for invalid cases.
- PDF Opening: The intent uses
ACTION_VIEWwith the PDF URL. It first checks if there's a compatible app (like a PDF viewer or browser) to handle the request, preventing crashes if no app is available. - HTTPS: Ensure your PDF links use
httpsinstead ofhttpto comply with Android's cleartext traffic policies (for API level 28+).
内容的提问来源于stack exchange,提问作者sam
相关产品推荐
相关产品推荐

