我正在處理我的主要項目,並且希望我的按鈕可以根據獲取的字符串數組值打開不同的意圖。我在按鈕的OnClickListener中使用if和else if語句,但不會再點擊。請幫幫我。如何在Android中按鈕的OnClickListener中添加if語句
這是我的XML文件:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardCornerRadius="3dp"
app:cardElevation="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/spacing_large">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/spacing_middle"
android:text="Syllabus"
android:textAppearance="@style/TextAppearance.AppCompat.Title" />
<Button
android:id="@+id/buttonpdf"
style="@style/Widget.AppCompat.Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="Open Book" />
</LinearLayout>
這是相同的活動Java文件:
public class ActivityBookDetails extends AppCompatActivity {
public static final String EXTRA_OBJCT = "com.app.sample.recipe.OBJ";
private Book book;
private FloatingActionButton fab;
private View parent_view;
Button button;
String[] obj;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_book_details);
parent_view = findViewById(android.R.id.content);
button = (Button)findViewById(R.id.buttonpdf);
book = (Book) getIntent().getSerializableExtra(EXTRA_OBJCT);
fab = (FloatingActionButton) findViewById(R.id.fab);
fabToggle();
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(book.getName());
((ImageView) findViewById(R.id.image)).setImageResource(book.getPhoto());
LinearLayout subjects = (LinearLayout) findViewById(R.id.subjects);
final String[] title_subjects = getResources().getStringArray(R.array.Subjects);
addIngredientsList(subjects, title_subjects);
obj = title_subjects;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ("Soft Computing".equals(obj)) {
Intent intent = new Intent(ActivityBookDetails.this, pdfviewactivity.class);
startActivity(intent);
}
else if ("Web Engineering".equals(obj)) {
Intent intent = new Intent(ActivityBookDetails.this, pdfweben.class);
startActivity(intent);
}
else if ("Network Management".equals(obj)) {
Intent intent = new Intent(ActivityBookDetails.this, pdfnetwork.class);
startActivity(intent);
}
else if ("Wireless Network".equals(obj)) {
Intent intent = new Intent(ActivityBookDetails.this, pdfwireless.class);
startActivity(intent);
}
}
});
我想我如果其他條件的值的基礎上開展工作字符串數組。
這是給定的意圖的一個樣本的Java文件:
public class pdfwireless extends AppCompatActivity {
PDFView pdfView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdfwireless);
pdfView = (PDFView)findViewById(R.id.pdfView);
pdfView.fromAsset("wirelessnetwork.pdf").load();
}
}
調試它並檢查'obj'的值 –