android实现点击TextView显示隐藏控件

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

实用代码分享,实现点击几次某个控件,显示隐藏功能。代码很简单但却很实用,比如可以实现测试环境和正式环境的切换功能,隐藏提交应用日志等等。

直接上代码

Activity

public class MainActivity extends AppCompatActivity {
    //全局属性
    private TextView mTitleView;
    private Spinner mSpinnerView;
    private int touchCount = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setListener();
    }


    private void initView(){
        mSpinnerView = (Spinner) findViewById(R.id.spinner);
        mSpinnerView.setVisibility(View.INVISIBLE);
        mTitleView = (TextView) findViewById(R.id.textViewTitle);
    }

    private void setListener(){
        mTitleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                touchCount += 1;
                if (touchCount == 5) {
                    mSpinnerView.setVisibility(View.VISIBLE);
                    touchCount = 0;

                } else {
                    Toast.makeText(getApplicationContext(), "Click more  " + Integer.toString(5 - touchCount), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

代码中设置spinner值,并设置监听事件

       //赋值
        String[] arrayStr= { "111", "222", "333", "444", "555" }; //定义数组
        //将可选内容与ArrayAdapter连接,
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, arrayStr);
        mSpinnerView.setAdapter(adapter);
        //设置监听事件
        mSpinnerView.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                String selectEmail = getResources().getStringArray(R.array.select_email)[position];
                String usename = userNameArray[position];
                mEtEmail.setText(selectEmail);
                mEtName.setText(usename);
            }

            @Override
            public void onNothingSelected(AdapterView parent) {

            }
        });

布局文件



    
    

资源文件



    
        1
        2
        3
        4
        5
    

over

转载于:https://my.oschina.net/u/244918/blog/682800

你可能感兴趣的:(android实现点击TextView显示隐藏控件)