我创建了一个简单的应用,但现在我想添加可跳转的标签。通过查看一些教程我必须使用标签,但我不知道如何为不同的标签插入单独的布局。任何人都可以帮我一个教程吗?
MainActivity.java
package com.example.cgpa;
import com.example.gpa.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter1 mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Semester 1", "Semester 2", "Semester 3" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter1(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
我认为现在一团糟。
我想将以下代码添加到第一个标签
public class MainActivity extends Activity implements OnClickListener {
TextView e1;
int g1, g2, g3, g4, g5, g6, g7, g8, g9, cr = 0;
int co1 = 0, co2 = 0, co3 = 0, co4 = 0, co5 = 0, co6 = 0, co7 = 0, co8 = 0,
co9 = 0;
String an, ch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
final Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
final Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);
final Spinner spinner4 = (Spinner) findViewById(R.id.spinner4);
final Spinner spinner5 = (Spinner) findViewById(R.id.spinner5);
final Spinner spinner6 = (Spinner) findViewById(R.id.spinner6);
final Spinner spinner7 = (Spinner) findViewById(R.id.spinner7);
final Spinner spinner8 = (Spinner) findViewById(R.id.spinner8);
final Spinner spinner9 = (Spinner) findViewById(R.id.spinner9);
Button b = (Button) findViewById(R.id.button1);
e1 = (TextView) findViewById(R.id.editText1);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String i1 = spinner1.getSelectedItem().toString();
if ("S".equals(i1)) {
g1 = 10;
co1++;
if (co1 == 1)
cr = cr + 4;
} else if ("A".equals(i1)) {
g1 = 9;
co1++;
if (co1 == 1)
cr = cr + 4;
} else if ("B".equals(i1)) {
g1 = 8;
co1++;
if (co1 == 1)
cr = cr + 4;
} else if ("C".equals(i1)) {
g1 = 7;
co1++;
if (co1 == 1)
cr = cr + 4;
} else if ("D".equals(i1)) {
g1 = 6;
co1++;
if (co1 == 1)
cr = cr + 4;
} else if ("E".equals(i1)) {
g1 = 5;
co1++;
if (co1 == 1)
cr = cr + 4;
} else if ("U".equals(i1)) {
if (co1 >=1) {
cr = cr - 4;
co1 = 0;
}
g1 = 0;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String i2 = spinner2.getSelectedItem().toString();
if ("S".equals(i2)) {
g2 = 10;
co2++;
if (co2 == 1)
cr = cr + 4;
} else if ("A".equals(i2)) {
g2 = 9;
co2++;
if (co2 == 1)
cr = cr + 4;
} else if ("B".equals(i2)) {
g2 = 8;
co2++;
if (co2 == 1)
cr = cr + 4;
} else if ("C".equals(i2)) {
g2 = 7;
co2++;
if (co2 == 1)
cr = cr + 4;
} else if ("D".equals(i2)) {
g2 = 6;
co2++;
if (co2 == 1)
cr = cr + 4;
} else if ("E".equals(i2)) {
g2 = 5;
co2++;
if (co2 == 1)
cr = cr + 4;
} else if ("U".equals(i2)) {
if (co2 >= 1) {
cr = cr - 4;
co2 = 0;
}
g2 = 0;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String i3 = spinner3.getSelectedItem().toString();
if ("S".equals(i3)) {
g3 = 10;
co3++;
if (co3 == 1)
cr = cr + 3;
} else if ("A".equals(i3)) {
g3 = 9;
co3++;
if (co3 == 1)
cr = cr + 3;
} else if ("B".equals(i3)) {
g3 = 8;
co3++;
if (co3 == 1)
cr = cr + 3;
} else if ("C".equals(i3)) {
g3 = 7;
co3++;
if (co3 == 1)
cr = cr + 3;
} else if ("D".equals(i3)) {
g3 = 6;
co3++;
if (co3 == 1)
cr = cr + 3;
} else if ("E".equals(i3)) {
g3 = 5;
co3++;
if (co3 == 1)
cr = cr + 3;
} else if ("U".equals(i3)) {
if (co3 >= 1) {
cr = cr - 3;
co3 = 0;
}
g3 = 0;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spinner4.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String i4 = spinner4.getSelectedItem().toString();
if ("S".equals(i4)) {
g4 = 10;
co4++;
if (co4 == 1)
cr = cr + 3;
} else if ("A".equals(i4)) {
g4 = 9;
co4++;
if (co4 == 1)
cr = cr + 3;
} else if ("B".equals(i4)) {
g4 = 8;
co4++;
if (co4 == 1)
cr = cr + 3;
} else if ("C".equals(i4)) {
g4 = 7;
co4++;
if (co4 == 1)
cr = cr + 3;
} else if ("D".equals(i4)) {
g4 = 6;
co4++;
if (co4 == 1)
cr = cr + 3;
} else if ("E".equals(i4)) {
g4 = 5;
co4++;
if (co4 == 1)
cr = cr + 3;
} else if ("U".equals(i4)) {
g4 = 0;
if (co4 >= 1) {
cr = cr - 3;
co4 = 0;
}
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spinner5.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String i5 = spinner5.getSelectedItem().toString();
if ("S".equals(i5)) {
g5 = 10;
co5++;
if (co5 == 1)
cr = cr + 3;
} else if ("A".equals(i5)) {
g5 = 9;
co5++;
if (co5 == 1)
cr = cr + 3;
} else if ("B".equals(i5)) {
g5 = 8;
co5++;
if (co5 == 1)
cr = cr + 3;
} else if ("C".equals(i5)) {
g5 = 7;
co5++;
if (co5 == 1)
cr = cr + 3;
} else if ("D".equals(i5)) {
g5 = 6;
co5++;
if (co5 == 1)
cr = cr + 3;
} else if ("E".equals(i5)) {
g5 = 5;
co5++;
if (co5 == 1)
cr = cr + 3;
} else if ("U".equals(i5)) {
g5 = 0;
if (co5 >= 1) {
cr = cr - 3;
co5 = 0;
}
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spinner6.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String i6 = spinner6.getSelectedItem().toString();
if ("S".equals(i6)) {
g6 = 10;
co6++;
if (co6 == 1)
cr = cr + 4;
} else if ("A".equals(i6)) {
g6 = 9;
co6++;
if (co6 == 1)
cr = cr + 4;
} else if ("B".equals(i6)) {
g6 = 8;
co6++;
if (co6 == 1)
cr = cr + 4;
} else if ("C".equals(i6)) {
g6 = 7;
co6++;
if (co6 == 1)
cr = cr + 4;
} else if ("D".equals(i6)) {
g6 = 6;
co6++;
if (co6 == 1)
cr = cr + 4;
} else if ("E".equals(i6)) {
g6 = 5;
co6++;
if (co6 == 1)
cr = cr + 4;
} else if ("U".equals(i6)) {
g6 = 0;
if (co6 >= 1) {
cr = cr - 4;
co6 = 0;
}
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spinner7.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String i7 = spinner7.getSelectedItem().toString();
if ("S".equals(i7)) {
g7 = 10;
co7++;
if (co7 == 1)
cr = cr + 2;
} else if ("A".equals(i7)) {
g7 = 9;
co7++;
if (co7 == 1)
cr = cr + 2;
} else if ("B".equals(i7)) {
g7 = 8;
co7++;
if (co7 == 1)
cr = cr + 2;
} else if ("C".equals(i7)) {
g7 = 7;
co7++;
if (co7 == 1)
cr = cr + 2;
} else if ("D".equals(i7)) {
g7 = 6;
co7++;
if (co7 == 1)
cr = cr + 2;
} else if ("E".equals(i7)) {
g7 = 5;
co7++;
if (co7 == 1)
cr = cr + 2;
} else if ("U".equals(i7)) {
g7 = 0;
if (co7 >= 1) {
cr = cr - 2;
co7 = 0;
}
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spinner8.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String i8 = spinner8.getSelectedItem().toString();
if ("S".equals(i8)) {
g8 = 10;
co8++;
if (co8 == 1)
cr = cr + 2;
} else if ("A".equals(i8)) {
g8 = 9;
co8++;
if (co8 == 1)
cr = cr + 2;
} else if ("B".equals(i8)) {
g8 = 8;
co8++;
if (co8 == 1)
cr = cr + 2;
} else if ("C".equals(i8)) {
g8 = 7;
co8++;
if (co8 == 1)
cr = cr + 2;
} else if ("D".equals(i8)) {
g8 = 6;
co8++;
if (co8 == 1)
cr = cr + 2;
} else if ("E".equals(i8)) {
g8 = 5;
co8++;
if (co8 == 1)
cr = cr + 2;
} else if ("U".equals(i8)) {
g8 = 0;
if (co8 >= 1) {
cr = cr - 2;
co8 = 0;
}
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spinner9.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String i9 = spinner9.getSelectedItem().toString();
if ("S".equals(i9)) {
g9 = 10;
co9++;
if (co9 == 1)
cr = cr + 1;
} else if ("A".equals(i9)) {
g9 = 9;
co9++;
if (co9 == 1)
cr = cr + 1;
} else if ("B".equals(i9)) {
g9 = 8;
co9++;
if (co9 == 1)
cr = cr + 1;
} else if ("C".equals(i9)) {
g9 = 7;
co9++;
if (co9 == 1)
cr = cr + 1;
} else if ("D".equals(i9)) {
g9 = 6;
co9++;
if (co9 == 1)
cr = cr + 1;
} else if ("E".equals(i9)) {
g9 = 5;
co9++;
if (co9 == 1)
cr = cr + 1;
} else if ("U".equals(i9)) {
g9 = 0;
if (co9 >= 1) {
cr = cr - 1;
co9 = 0;
}
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int a = (g1 * 4 + g2 * 4 + g3 * 3 + g4 * 3 + g5 * 3 + g6 * 4
+ g7 * 2 + g8 * 2 + g9 * 1);
double e11 = a / (float) cr;
DecimalFormat df = new DecimalFormat("#.00");
an = df.format(e11);
ch = String.valueOf(an);
e1.setText("Your GPA is " + ch);
}
});
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
我想为第一个布局添加这个xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="match_parent"
android:layout_height="753dp"
android:background="@color/orange" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="90dp"
android:layout_marginTop="120dp"
android:text="HS6151"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="90dp"
android:layout_marginTop="165dp"
android:text="PH6151"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="90dp"
android:layout_marginTop="210dp"
android:text="CY6151"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="90dp"
android:layout_marginTop="255dp"
android:text="GE6151"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="90dp"
android:layout_marginTop="300dp"
android:text="GE6152"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="90dp"
android:layout_marginTop="345dp"
android:text="GE6161"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="90dp"
android:layout_marginTop="390dp"
android:text="GE6162"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="90dp"
android:layout_marginTop="435dp"
android:text="GE6163"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="200dp"
android:layout_marginTop="65dp"
android:entries="@array/Grade"
android:prompt="@string/hello_world"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="200dp"
android:layout_marginTop="110dp"
android:entries="@array/Grade"
android:prompt="@string/hello_world"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinner3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="200dp"
android:layout_marginTop="155dp"
android:entries="@array/Grade"
android:prompt="@string/hello_world"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinner4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="200dp"
android:layout_marginTop="200dp"
android:entries="@array/Grade"
android:prompt="@string/hello_world"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinner5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="200dp"
android:layout_marginTop="245dp"
android:entries="@array/Grade"
android:prompt="@string/hello_world"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinner6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="200dp"
android:layout_marginTop="290dp"
android:entries="@array/Grade"
android:prompt="@string/hello_world"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinner7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="200dp"
android:layout_marginTop="335dp"
android:entries="@array/Grade"
android:prompt="@string/hello_world"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinner8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="200dp"
android:layout_marginTop="380dp"
android:entries="@array/Grade"
android:prompt="@string/hello_world"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinner9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="200dp"
android:layout_marginTop="425dp"
android:entries="@array/Grade"
android:prompt="@string/hello_world"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner9"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="calculate" />
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="SEMESTER I - GPA CALCULATOR"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp" />
<TextView
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="1dp"
android:editable="false"
android:ems="10"
android:gravity="center"
android:inputType="number"
android:textSize="20dp" />
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="90dp"
android:layout_marginTop="75dp"
android:text="MA6151"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</ScrollView>
这是我当前的activity_main.xml。我不知道该怎么做。
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
答案 0 :(得分:0)
我会向你推荐这个很棒的图书馆:https://github.com/astuetz/PagerSlidingTabStrip
答案 1 :(得分:0)
假设您已经正确实施了viewpager
,如果您想为不同的标签添加不同的片段,则需要更改FragmentStatePagerAdapter
的行为。
@Override
public Fragment getItem(int position) {
if (position == 1) {
return new SomeFragment();
} else if (position == 2) {
return new SomeOtherFragment();
} else {
return new someOtherO
}
}