我需要在哪里放置Java代码来对选项卡式活动中的按钮进行编程?在包含这两个选项卡的活动中,我有以下代码:
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;
}
}
这些是我想要对按钮进行编程的选项卡的xml:
<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" />
答案 0 :(得分:0)
您必须编码您在活动中使用的Fragment类中的按钮。在您的情况下,您将必须添加和编码Tab1tests和Tab2calendar类中的按钮,而不是像您一样在活动本身中编码。