gpt4 book ai didi

android - 如何在ViewPager中编写按钮点击事件

转载 作者:行者123 更新时间:2023-11-29 16:10:08 25 4
gpt4 key购买 nike

这里我的 activity_main.xml 存在于/res/layout

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.


-->

<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />

</android.support.v4.view.ViewPager>

这是我的 MainActivity.java 文件

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;

import android.view.ViewGroup;

public class MainActivity extends FragmentActivity {

SectionsPagerAdapter mSectionsPagerAdapter;

/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}

@Override
public int getCount() {
// Show 3 total pages.
return 2;
}

@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "POS 0"; // getString(R.string.title_section1).toUpperCase();
case 1:
return "POS 1";// getString(R.string.title_section2).toUpperCase();
case 2:
return "POS 2";// getString(R.string.title_section3).toUpperCase();
}
return null;
}
}

/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";

public DummySectionFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view;
String i = Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER));

if (i.toString() == Integer.toString(1)) {
view = inflater.inflate(R.layout.section2, container, false);
} else {
view = inflater.inflate(R.layout.section1, container, false);


// Button button = (Button) view.findViewById(R.id.button1);
// button.setOnClickListener(new OnClickListener() {
// if i uncomment this code, button click works but i want it to handle in section1.java file
// @Override
// public void onClick(View v) {
// Activity activity = getActivity();
//
// if (activity != null) {
// Toast.makeText(activity, "sdfsdfsdfds",
// Toast.LENGTH_LONG).show();
// }
// }
//
// });

}

return view;

}
}

}

我有一个名为“Section1”的 Activity ,当我向右滑动时会显示该 Activity 。它上面有一个按钮。如何处理 section1.java 文件中的按钮点击事件?

这是我的 section.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Section1" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="Button" />

</RelativeLayout>

这是 Section1.Java 文件。这里的按钮即使没有触发也可以点击。

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class Section1 extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.section1);

}

public void sendMessage(View view) {
Toast t = Toast.makeText(getApplicationContext(), "sdfsdfsdf",
Toast.LENGTH_LONG);
t.show();
}

}

最佳答案

我和你有同样的问题,我今天终于弄明白了,所以我想我会和你分享

public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int i) {
switch (i) {
case 2:
// The first section of the app is the most interesting -- it offers
// a launchpad into the other demonstrations in this example application.
return new LessonFragment();

default:
// The other sections of the app are dummy placeholders.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
}


@Override
public int getCount() {
return 4;
}

@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return getString(R.string.title_section1).toUpperCase();
case 1: return getString(R.string.title_section2).toUpperCase();
case 2: return getString(R.string.title_section3).toUpperCase();
case 3: return getString(R.string.title_section4).toUpperCase();
}
return null;
}
}




/**
* A fragment that launches other parts of the demo application.
*/
public static class LessonFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.lesson, container, false);

// Demonstration of a collection-browsing activity.
rootView.findViewById(R.id.button1)
.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Test.class);
startActivity(intent);
}
});



return rootView;
}
}

关于android - 如何在ViewPager中编写按钮点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14139300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com