2016-01-20 123 views
0

對不起,這可能是英文..真的需要幫助!選項卡式活動片段中的訪問按鈕

我剛剛在幾天前開始探索android工作室,所以我還是新的。 我的問題是我無法訪問我在選項卡式活動中創建的按鈕。 IM採用了android工作室1.5.1.Below是最終輸出:

enter image description here

,你可以看到,我設法把按鈕的標籤內,但我不能讓他們做的動作,因爲我不知道如何訪問他們..我希望他們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; 
 
     } 
 
    } 
 

 

 
}

回答

0

你現在正在做的權利,只是充氣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; 
     }