2017-07-08 98 views
0

我需要將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" /> 

} This layout

+0

的按鈕,你說什麼?你正在使用標籤式活動,您已經定義date_button –

+0

是的,但因爲是從片段活動Tab1testes –

回答

0

您將不得不編碼您在活動中使用的Fragment類中的按鈕。在你的情況下,你將不得不添加和編寫Tab1tests和Tab2calendar類中的按鈕,而不是像你所做的那樣在活動本身中編寫按鈕。

+0

而當它需要的參數範圍內的類,什麼是date_button顯示爲空?在活動本身它只是Testes.this,但在Tab1tests片段中它不接受Tab1testes.this –

+0

嘗試使用getActivity()作爲參數上下文。 –

+0

它的工作表示感謝!現在即時通訊運行我的模擬器,看看是否將date_buton代碼放在​​片段中工作 –

相關問題