Create a new class, name it third.
Right click to folder layout, New
> Android XML File, name it ac3, finish.
Edit code to make it look like this.
public class third extends Activity {
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac3);
}
}
Copy in to file ac3.xml
<?xml
version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:text="Lunar year:"
android:textColor="#800000"
android:textSize="20sp"
/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:textColor="#000080"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:text="Choose year:"
android:textColor="#800000"
android:textSize="20sp"
/>
<Spinner
android:id="@+id/spin1"
android:layout_width="72dp"
android:layout_height="45dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:background="#a1caf1"
android:lineSpacingExtra="4dp"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="41dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
>
<Button
android:id="@+id/bu1"
android:layout_width="65dp"
android:layout_height="40dp"
android:layout_marginLeft="4dp"
android:text="Quit"
android:textColor="#0000cd"
android:textSize="14sp"
/>
</LinearLayout>
</LinearLayout>
Double click to file
AndroidManifest.xml to declare new class.
In class third, declare textView,
Spinner, Button, find address.
Spinner show a list of data items,
we can choose what we need, no need enter from keyboard.
Declare an array and adapter.
String[] data = { "2010", "2011", "2012", "2013", "2014",
"2015", "2016", "2017", "2018", "2019", "2020"};
private
ArrayAdapter<String> adapter;
Import library.
Code now look like this.
Copy to below findViewById.
adapter = new ArrayAdapter<String>(third.this,
android.R.layout.simple_spinner_item,data);
sp.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String year = sp.getSelectedItem().toString();
}
@Override
public void
onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
sp.setAdapter(adapter);
Here we set adapter for spinner to
make it show list, class now look like this.
Back to MainActivity change code in
Next button to.
Intent in = new Intent(MainActivity.this,third.class);
startActivity(in);
Run to see new class show spinner.
Copy function change year to above
last close bracket.
public String lunaryear(int year){
int remain=year%12;
String lunar="";
if(remain==1){
lunar="Rooster";
}
else if(remain==2){
lunar="Dog";
}
else if(remain==3){
lunar="Pig";
}
else if(remain==4){
lunar="Rat";
}
else if(remain==5){
lunar="Cow";
}
else if(remain==6){
lunar="Tiger";
}
else if(remain==7){
lunar="Rabbit";
}
else if(remain==8){
lunar="Dragon";
}
else if(remain==9){
lunar ="Snake";
}
else if(remain==10){
lunar="Horse";
}
else if(remain==11){
lunar="Goat";
}
else {
lunar="Monkey";
}
return lunar;
}
In Spinner, we get string user
choose by.
String year = sp.getSelectedItem().toString();
We cast it to number by this.
int year2 = Integer.parseInt(year);
No need try catch because our data not contain text inside.
After that, call function to change it to lunar year, set result
to textView.
String lunar = lunaryear(year2);
tv.setText(lunar);
Run to see result.
Now we want spinner show year 2018
when open screen. Add this line to below setAdapter.
sp.setSelection(8);
Value 2018 is the eighth element, so
we set i=7 because i=0 mean first element.
Code Quit button with command
finish().
If our data array contain blank
character in numbers, we can use trim() to remove them.
year=year.trim();
Change font size, text color in file
ac3.xml for more practical.
To change Hex code in line android:textColor="#0000cd"
search Hex code color online.
No comments:
Post a Comment