简体   繁体   中英

Programming tabbed activity content

Where do I need to put the Java code to program the buttons in a tabbed activity? In the activity that contains those two tabs I have this code:

   public class Testes extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;


private ViewPager mViewPager;
private DatePickerDialog.OnDateSetListener hourSetListener;
private DatePickerDialog.OnDateSetListener dateSetListener;
private Button date_button;
private Button hora_picker;
private TextView date_text;
private TextView hora_text;


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

    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);

    date_button = (Button) findViewById(R.id.date_button);

    date_button.setOnClickListener(
            new View.OnClickListener(){
                public void onClick(View v){
                        Calendar cal = Calendar.getInstance();
                        int year = cal.get(Calendar.YEAR);
                        int month = cal.get(Calendar.MONTH);
                        int day = cal.get(Calendar.DAY_OF_MONTH);
                        DatePickerDialog date_dialog = new DatePickerDialog(
                                Testes.this,
                                android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                                hourSetListener,
                                year, month, day);
                        date_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                        date_dialog.show();


                    };
                }

    );

    dateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int month, int day) {
            month=month+1;
            date_button.setVisibility(View.GONE);
            date_text = (TextView) findViewById(R.id.date_text);
            date_text.setVisibility(View.VISIBLE);
            String dt = day+"-"+month+"-"+year;
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
            try {
                sdf.parse(dt);
                date_text.setText(dt);
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
    };




}




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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()){
       case go_to_fazer_media:{
           if (item.isChecked())
               item.setChecked(false);
            else{
           Intent i = new Intent(Testes.this,Formas.class);
           startActivity(i);
               return true;
            }
       }
       case definicoes:{
           if (item.isChecked())
               item.setChecked(false);
           else{
              return true;
           }
       }
   }


    return super.onOptionsItemSelected(item);
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        switch(position){

            case 0:
                Tab1tests tab1 =  new Tab1tests();
                return tab1;
            case 1:
                Tab2calendar tab2 =  new Tab2calendar();
                return tab2;
            default:
                return null;
        }
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Adicionar Testes";
            case 1:
                return "Mapa de Testes";
        }
        return null;
    }
}

These is the xml of the tab that I want to program the buttons:

   <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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<TextView
    android:id="@+id/section_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/disciplina"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/section_label"
    android:layout_margin="20dp"
    android:layout_toEndOf="@+id/section_label"
    android:text="Disciplina:"
    android:textSize="35sp" />

<TextView
    android:id="@+id/sala"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/disciplina"
    android:layout_margin="20dp"
    android:layout_toEndOf="@+id/section_label"
    android:text="Sala:"
    android:textSize="35sp"/>

<TextView
    android:id="@+id/Dia"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/sala"
    android:layout_margin="20dp"
    android:layout_toEndOf="@+id/section_label"
    android:text="Dia:"
    android:textSize="35sp" />

<TextView
    android:id="@+id/Hora"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/Dia"
    android:layout_margin="20dp"
    android:layout_toEndOf="@+id/section_label"
    android:text="Hora:"
    android:textSize="35sp" />

<Button
    android:id="@+id/add_test"
    style="@style/Widget.AppCompat.Button.Colored"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:layout_marginBottom="95dp"
    android:text="Adicionar o teste"
    android:textSize="20sp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:id="@+id/date_text"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/hora_picker"
    android:layout_alignTop="@+id/Dia"
    android:text="ESCOLHER DATA"
    android:textSize="25sp"
    android:textStyle="bold"
    android:visibility="invisible"/>

<Button
    android:id="@+id/date_button"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/hora_picker"
    android:layout_alignTop="@+id/Dia"
    android:text="ESCOLHER DATA"
    android:textSize="25sp"
    android:textStyle="bold"
    android:onClick="setDate"/>

<TextView
    android:id="@+id/hora_text"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:text="ESCOLHER HORA"
    android:textStyle="bold"
    android:textSize="25sp"
    android:layout_alignBottom="@+id/Hora"
    android:layout_toEndOf="@+id/Hora"
    android:visibility="invisible"/>
<Button
    android:id="@+id/hora_button"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:text="ESCOLHER HORA"
    android:textStyle="bold"
    android:textSize="25sp"
    android:layout_alignBaseline="@+id/Hora"
    android:layout_toEndOf="@+id/sala" />

<EditText
    android:id="@+id/sala_text"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/sala"
    android:layout_toEndOf="@+id/Hora"
    android:ems="10"
    android:hint="Nª/Nome da sala"
    android:textSize="25sp"
    android:inputType="textPersonName" />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/disciplina"
    android:layout_toEndOf="@+id/disciplina"
    android:scrollbarSize="25sp"
    android:layout_alignTop="@+id/disciplina" />

} This layout

You will have to code the buttons in the Fragment class which you are using in the activity. In your case you will have to add and code the buttons in the Tab1tests and Tab2calendar classes and not in the activity itself as you have done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM