我正在处理我的主要项目,并且希望我的按钮可以根据获取的字符串数组值打开不同的意图。我在按钮的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'的值 –