Interface programming and layout of Android reading notes

interface design:

Related classes: Activity , Fragment , View , ViewGroup , Layout .

view:

简单控件:ButtonImageButtonToggleButtonTextViewEditTextRadioButtonCheckBoxImageViewProgressBarSeekBarRatingBar等。

Advanced controls: AutoCompleteTextView , Spinner , ListView , GridView , Gallery , etc.

View Group ( ViewGroup ): A complex view composed of multiple views, a subclass of View , and a parent class for advanced controls and layouts.

 

Interface construction:

Design tool: WYSIWYG visual interface design tool.

 

Control time processing:

Event source: each control.

Events: EG : KeyEvent , MotionEvent (encapsulated into a class) Special: Button click event is not encapsulated

Event handler: implement the XXXListenter interface --> event listener.

 

Listener:

Activity as event listener:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate (savedInstanceState);

        setContentView(R.layout.activity_main);

        // Get the OK button object by id

        Button btnOK = (Button) findViewById(R.id.button);

        // register event listener

        btnOK.setOnClickListener(this);

    }

 

    /*

     *  Implement the View.OnClickListener interface method

     */

    @Override

    public void onClick(View view) {

        TextView text = (TextView) findViewById(R.id.textView);

        text.setText("HelloWorld");

    }

}

 

 

 

 

 

Inner class event listener:

public class MainActivity extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate (savedInstanceState);

        setContentView(R.layout.activity_main);

        // Get the OK button object by id

        Button btnOK = (Button) findViewById(R.id.button);

        // register event listener

        btnOK.setOnClickListener(new ButtonOKOnClickListener());

    }

    class ButtonOKOnClickListener implements View.OnClickListener {

        /*

         *  Implement the View.OnClickListener interface method

         */

        @Override

        public void onClick(View view) {

            TextView text = (TextView) findViewById(R.id.textView);

            text.setText("HelloWorld");

        }

    }

}

Anonymous inner class event listener:

public class MainActivity extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate (savedInstanceState);

        setContentView(R.layout.activity_main);

        // Get the OK button object by id

        Button btnOK = (Button) findViewById(R.id.button);

        // register event listener

        btnOK.setOnClickListener(new View.OnClickListener() {

            /*

             *  Implement the View.OnClickListener interface method

             */

            @Override

            public void onClick(View v) {

                TextView text = (TextView) findViewById(R.id.textView);

                text.setText("HelloWorld");

            }

        });

    }

}

 

Handling of screen events: (touch events && keyboard events)

Touch event: // Override the method of the activity public boolean onTouchEvent(MotionEvent event)

// Display the current action and position on the screen

public class MainActivity extends AppCompatActivity {

// Two TextViews are displayed respectively: action, position

    private TextView mAction;

    private TextView mPostion;

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate (savedInstanceState);

        setContentView(R.layout.activity_main);

 

        mAction = (TextView) findViewById(R.id.action);

        mPostion = (TextView) findViewById (R.id.postion);

    }

// This method ( onTouchEvent() ) must be overridden

    @Override

    public boolean onTouchEvent(MotionEvent event) {

        int action = event.getAction();// Action Action is constant

 

        switch (action) {

            case MotionEvent.ACTION_UP:

                mAction.setText(" Finger up ");

                break;

            case MotionEvent.ACTION_DOWN:

                mAction.setText(" Finger press ");

                break;

            case MotionEvent.ACTION_MOVE:

                mAction.setText(" Finger move ");

        }

// Get X- axis, Y- axis coordinates

        float X = event.getX();

        float Y = event.getY();

        mPostion.setText("位置 = (" + X + "," + Y + ")");

 

        return true; // return true to execute this method, otherwise false

    }

}

Keyboard events:

//keyCode  is the code of the key; KeyEvent is the keyboard event

Boolean onKeyUp (int keyCode ,KeyEvent event)

Boolean onKeyDown (int keyCode ,KeyEvent event)

Boolean onKeyLongPress (int keyCode ,KeyEvent event)

The combination of the transparency of the picture and the sound button Demo :

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "keyeventsample";

    private ImageView mImage;

    private TextView mAlphavalueText;

    private int mAlphavalue; // Global variable, current image transparency: ( 0,255 )

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate (savedInstanceState);

        setContentView(R.layout.activity_main);

 

        mImage = (ImageView) findViewById(R.id.image);

        mAlphavalueText = (TextView) findViewById(R.id.alphavalue);

 

        mAlphavalue = 100; // The initial value of transparency is 100

        mImage.setImageAlpha(mAlphavalue); // Set the image transparency

        mAlphavalueText.setText("Alpha = " + mAlphavalue * 100 / 255 + "%");

 

    }

 

    @Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {

// listen for all keyboard events

        switch (keyCode) {

            case KeyEvent.KEYCODE_VOLUME_UP:// Amplify sound key

                mAlphavalue += 20;

                break;

            case KeyEvent.KEYCODE_VOLUME_DOWN:// scale down the sound key

                mAlphavalue -= 20;

                break;

        }

        if (mAlphavalue >= 255) {

            mAlphavalue = 255;

        }

        if (mAlphavalue <= 0) {

            mAlphavalue = 0;

        }

        mImage.setImageAlpha (mAlphavalue);

        mAlphavalueText.setText("Alpha = " + mAlphavalue * 100 / 255 + "%");

 

        return super.onKeyDown(keyCode, event);

    }

}




Purpose: Make reasonable use of screen space and adapt to different screens

The layout class is a container that inherits the ViewGroup class

Six basic layout classes:

FrameLayout/LinearLayout/AbsoluteLayout/RelativeLayout/TableLayout/GridLayout

 

FrameLayout : ( FrameLayout , frame layout) is not used very often, its subclasses are often used

子类:TextSwitcher/ImageSwitcher/DatePicker/ScrollView/TabHost

 

Linear Layout: The Most Common Layout

android:orientation=”vertical/horizontal” ( vertical arrangement, horizontal arrangement )

android:layout_weight=”X”

 

Relative layout: (layout code omitted)

padding : (padding)

margin : (outer margin)

 

Grid layout implementation:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_gravity="center_horizontal"

    android:columnCount="4"

    android:orientation="horizontal"

    android:rowCount="5">

 

    <Button

        android:id="@+id/zero"

        android:layout_columnSpan="2"

        android:layout_gravity="fill"

        android:text="0" />

    <Button

        android:id="@+id/point"

        android:text="." />                                 

    <Button

        android:id="@+id/plus"

        android:layout_gravity="fill"

        android:layout_rowSpan="2"

        android:text="+" />

    <Button

        android:id="@+id/equal"

        android:layout_columnSpan="3"

        android:layout_gravity="fill"

        android:text="=" />

</GridLayout>

Nesting of layouts:

<?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="match_parent"

        android:layout_height="wrap_content"

        android:gravity="center"

        android:text="@string/title"

        android:textSize="20sp" />

 

    <include

        android:id="@+id/include01"

        layout="@layout/layoutcase1" />

 

    <include

        android:id="@+id/include02"

        layout="@layout/layoutcase2" />

 

    <include

        android:id="@+id/include03"

        layout="@layout/layoutcase3" />

 

</LinearLayout>

// Faced with complex layout structures, nested applications are necessary ~

The problem of screen rotation : (Landscape: horizontal screen Portrait : vertical screen )

// Get the current device orientation state

this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT

Three states of the device:

Configuration.ORIENTATION_PORTRAIT // Constant, the device is in portrait state

Configuration.ORIENTATION_LANDSCAPE // Constant, the device is in landscape state

Configuration.ORIENTATION_UNDEFINED // Constant, the device is in an unknown state

 

Note: setContentView () in Activity can reload the layout file.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324469347&siteId=291194637