Wednesday, February 20, 2019

Lesson 2– New Activity, EditText, Toast

Right click to package name, New> Class, name it second, finish.

Right click to folder layout, New > Android XML File, name it ac2, finish. 

If screen look like this:

Click to ac2 in red circle.
Copy in to class second.
public class second extends Activity {
         
          @Override
          protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.ac2);

}

See the line setContenView is R.layout.ac2, we assign class to layout in that we design user interface.
Copy to file ac2.xml
<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:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:text="Enter calendar year:"
        android:textColor="#800000"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/e"
        android:layout_width="82dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:background="#ccccff"
        android:gravity="center"
        android:hint="Calendar"
        android:lineSpacingExtra="4dp"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#00F"
        android:textSize="14sp" >
    </EditText>

    <TextView
        android:id="@+id/tv2"
        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="20dp" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:textColor="#000080"
        android:textSize="20dp" />

    <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="85dp"
            android:layout_height="40dp"
            android:text="Change"
            android:textColor="#0000cd"
            android:textSize="14sp" />

        <Button
            android:id="@+id/bu2"
            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, copy to above </application>.
          <activity
          android:name="com.bai.baidau.second"           
          android:screenOrientation="portrait">
           </activity>

Use your package name instead.
We declare new class and set screen to portrait.
Back to class MainActivity, copy code for Next button.
b2.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
Intent in = new Intent(MainActivity.this,second.class);                
startActivity(in);
}
});
Run, press Next to open class second, screen like this.


Now we code in class second, make a programme to change calendar year to lunar year.
Declare TextView, Edittext, Button, find address.
Add this line: e.setRawInputType(Configuration.KEYBOARD_QWERTY); to show numberphone pad when user touch EditText.

Code for Quit button first.
b2.setOnClickListener(new View.OnClickListener() {
                     @Override
                     public void onClick(View v) {
                          finish();
                     }
               });
This code close current screen, back to previous screen.
Button year change, we need to do:
-Check if nothing enter Edittext, show notification.
-Check if data enter is number or not.
-Change to lunar year.
We get string enter by String year = e.getText().toString();
Check if it blank or not by if (year == null || year.trim().equals(""))
We use try catch to cast enter string to number, if it contain text, show toast.
If it is number, change to lunar year by call this method.
Copy 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;
     }
After that, we set text to textView tv.
Code look like this.

Run to see result.


No comments:

Post a Comment