选项卡式活动片段中的访问按钮
问题描述:
对不起,这可能是英文..真的需要帮助!选项卡式活动片段中的访问按钮
我刚刚在几天前开始探索android工作室,所以我还是新的。 我的问题是我无法访问我在选项卡式活动中创建的按钮。 IM采用了android工作室1.5.1.Below是最终输出:
,你可以看到,我设法把按钮的标签内,但我不能让他们做的动作,因为我不知道如何访问他们..我希望他们poput我已经创建的警报对话框。
这是所有我做的代码:
akauninduk.java
package com.example.asus.gorunnerapp;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class akauninduk extends AppCompatActivity implements View.OnClickListener {
/**
* The {@link PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_akauninduk);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//String string = getResources().getString(R.string.title_activity_akauninduk);
getMenuInflater().inflate(R.menu.menu_akauninduk, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView;
if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
rootView = inflater.inflate(R.layout.fragment_akauninduk, container, false);
} else
rootView = inflater.inflate(R.layout.fragment_akauninduk2, container, false);
return rootView;
}
}
/**
* 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 PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "PENGAGIHAN";
case 1:
return "SENARAI PERMOHONAN";
}
return null;
}
}
}
这是XML文件,使所有按钮的图像上:fragment_akauninduk。 xml
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.asus.gorunnerapp.akauninduk$PlaceholderFragment">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="108dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="JPMM"
android:id="@+id/textView11"
android:layout_below="@+id/section_label"
android:layout_toRightOf="@+id/section_label"
android:layout_toEndOf="@+id/section_label"
android:textSize="25dp"
android:textColor="#000000"
android:textAlignment="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="RM15,000/RM1000"
android:id="@+id/textView12"
android:textAlignment="center"
android:textColor="#2618a1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pilih Menu"
android:id="@+id/bButton1"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="108dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="NADI"
android:id="@+id/textView9"
android:layout_below="@+id/section_label"
android:layout_toRightOf="@+id/section_label"
android:layout_toEndOf="@+id/section_label"
android:textSize="25dp"
android:textColor="#000000"
android:textAlignment="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="RM15,000/RM1000"
android:id="@+id/textView10"
android:textAlignment="center"
android:textColor="#2618a1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pilih Menu"
android:id="@+id/bButton2"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="108dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="MAYANG"
android:id="@+id/textView7"
android:layout_below="@+id/section_label"
android:layout_toRightOf="@+id/section_label"
android:layout_toEndOf="@+id/section_label"
android:textSize="25dp"
android:textColor="#000000"
android:textAlignment="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="RM15,000/RM1000"
android:id="@+id/textView8"
android:textAlignment="center"
android:textColor="#2618a1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pilih Menu"
android:id="@+id/bButton3"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="108dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="UMNO"
android:id="@+id/textView5"
android:layout_below="@+id/section_label"
android:layout_toRightOf="@+id/section_label"
android:layout_toEndOf="@+id/section_label"
android:textSize="25dp"
android:textColor="#000000"
android:textAlignment="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="RM15,000/RM1000"
android:id="@+id/textView6"
android:textAlignment="center"
android:textColor="#2618a1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pilih Menu"
android:id="@+id/bButton4"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
</LinearLayout>
</LinearLayout>
我创建了一个新的java类来处理fragment_akauninduk.xml文件,并且我已经制作了这个函数,以便按钮在单击时工作,但我没有工作。
这里是java类处理fragment_akauninduk.xml文件:
handletab.java
package com.example.asus.gorunnerapp;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Toast;
/**
* Created by Asus on 21/1/2016.
*/
public class handletab extends AppCompatActivity implements View.OnClickListener {
Button bButton1;
Button bButton2;
Button bButton3;
Button bButton4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_akauninduk);
bButton1 = (Button) findViewById(R.id.bButton1);
bButton2 = (Button) findViewById(R.id.bButton2);
bButton3 = (Button) findViewById(R.id.bButton3);
bButton4 = (Button) findViewById(R.id.bButton4);
bButton1.setOnClickListener(this);
bButton2.setOnClickListener(this);
bButton3.setOnClickListener(this);
bButton4.setOnClickListener(this);
final String[] option = new String[] { "Add", "View", "Change", "Delete", "Delete", "Delete", "Delete", "Delete", "Delete", "View", "Change", "Delete", "Delete", "Delete", "Delete", "Delete", "Delete" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, option);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Option");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),option[which], Toast.LENGTH_SHORT).show();
bButton1 = (Button) findViewById(R.id.bButton1);
bButton1.setText(option[which]);
}
});
final AlertDialog dialog = builder.create();
//Spinner spinner = (Spinner) findViewById(R.id.spinner_control);
bButton1 = (Button) findViewById(R.id.bButton1);
bButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//String string = getResources().getString(R.string.title_activity_akauninduk);
getMenuInflater().inflate(R.menu.menu_akauninduk, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void showJPMM() {
final String[] option = new String[]{"Add", "View", "Change", "Delete", "Delete", "Delete", "Delete", "Delete", "Delete", "View", "Change", "Delete", "Delete", "Delete", "Delete", "Delete", "Delete"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, option);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pilih Menu");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), option[which], Toast.LENGTH_SHORT).show();
bButton1 = (Button) findViewById(R.id.bButton1);
bButton1.setText(option[which]);
}
});
final AlertDialog dialog = builder.create();
//Spinner spinner = (Spinner) findViewById(R.id.spinner_control);
bButton1 = (Button) findViewById(R.id.bButton1);
dialog.show();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bButton1:
showJPMM();
break;
}
}
}
答
你现在正在做的权利,只是充气2个不同的布局,因此您无法访问和使用它。你真正需要做的是创建2个片段(新的>片段>片段(空白)),然后在方法getItem(position)中调用它,如下面的代码。然后你让每个选项卡在片段文件中做他们的东西。
@Override
public Fragment getItem(int position) {
if(position == 0){
FragmentPengagihan fragmentPengagihan = new FragmentPengagihan();
return fragmentPengagihan;
}
else if(position == 1){
FragmentSenaraiPermohonan fragmentPermohonan = new FragmentSenaraiPermohonan();
return fragmentSenaraiPermohonan;
}
return null;
}