Android Studio click effects and events

This article is just to prevent forgetting and being lazy, excerpts from the main code

JAVA code:
public class SettingActivity extends Activity {
    private TextView tvSetLanguage;
    private TextView tvSetTheme;
    private TextView tvSetGuide;
    private TextView tvSetWe;
 @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(info.btsland.app.R.layout.activity_setting);
    }

    /**
     * initialize
     */
    private void init(){
        tvSetLanguage=findViewById(R.id.tv_set_language);
        tvSetTheme=findViewById(R.id.tv_set_theme);
        tvSetGuide=findViewById(R.id.tv_set_guide);
        tvSetWe=findViewById(R.id.tv_set_we);

        //Bind the special effect event
        TextViewOnTouchListener OnTouchListener=new TextViewOnTouchListener();
        tvSetLanguage.setOnTouchListener(OnTouchListener);
        tvSetTheme.setOnTouchListener(OnTouchListener);
        tvSetGuide.setOnTouchListener(OnTouchListener);
        tvSetWe.setOnTouchListener(OnTouchListener);

        //bind click event
        TextViewOnClickListener OnClickListener=new TextViewOnClickListener();
        tvSetLanguage.setOnClickListener(OnClickListener);
    }

    /**
     * Click effect
     * @param textView the clicked tv
     * @param motionEvent current state
     */
    protected void touchColor(TextView textView,MotionEvent motionEvent){
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            textView.setBackground(getResources().getDrawable(R.drawable.tv_row_touch,null));
        }
        if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
            textView.setBackground(getResources().getDrawable(R.drawable.tv_row,null));
        }
    }
    class TextViewOnTouchListener implements View.OnTouchListener{
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (view.getId()) {
                case R.id.tv_set_language:
                    touchColor(tvSetLanguage,motionEvent);
                    break;
                case R.id.tv_set_theme:
                    touchColor(tvSetTheme,motionEvent);
                    break;
                case R.id.tv_set_guide:
                    touchColor(tvSetGuide,motionEvent);
                    break;
                case R.id.tv_set_we:
                    touchColor(tvSetWe,motionEvent);
                    break;
            }
           //ture is to replace the click event, so set it to false
            return false;
        }
    }

    /*
     *click event
     */
    class TextViewOnClickListener implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.tv_set_language:
                    Intent intent=new Intent(SettingActivity.this,TextActivity.class);
                    startActivity(intent);

                    break;
                case R.id.tv_set_theme:
                    break;
                case R.id.tv_set_guide:
                    break;
                case R.id.tv_set_we:
                    break;
            }
        }
    }
}


layout file:
quote

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EEEEEE"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_set_language"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:text="语言"
        android:gravity="center"
        android:layout_margin="5dp"
        android:background="@drawable/tv_row"
        android:textColor="@color/color_white" />

</LinearLayout>


Guess you like

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