Android Studio 点击特效与事件

此文章只不过是防止忘记与偷懒而已,节选部分主要代码

JAVA代码:
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);
    }

    /**
     * 初始化
     */
    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);

        //绑定特效事件
        TextViewOnTouchListener OnTouchListener=new TextViewOnTouchListener();
        tvSetLanguage.setOnTouchListener(OnTouchListener);
        tvSetTheme.setOnTouchListener(OnTouchListener);
        tvSetGuide.setOnTouchListener(OnTouchListener);
        tvSetWe.setOnTouchListener(OnTouchListener);

        //绑定点击事件
        TextViewOnClickListener OnClickListener=new TextViewOnClickListener();
        tvSetLanguage.setOnClickListener(OnClickListener);
    }

    /**
     * 单击特效
     * @param textView 被单击的tv
     * @param motionEvent 当前状态
     */
    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为替换点击事件,所以设置成false
            return false;
        }
    }

    /*
     *点击事件
     */
    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;
            }
        }
    }
}


布局文件:
引用

<?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>


猜你喜欢

转载自wenweijie.iteye.com/blog/2396032