Android - AutoCompleteTextView onClickListener在第二次单击后工作

时间:2014-04-25 10:45:33

标签: android

我有四个AutoCompleteTextView(ACTV),他们工作得很好。我为他们创建了一个onClickListener(),以便知道用户何时在任何时间选择/按下四个中的任何一个。然而,经过仔细检查,我注意到我必须在触发之前点击两次ACTV。

这是我的代码:

在我的onCreate()中,我初始化了actv并设置了onClickListener()

brandACTV = (AutoCompleteTextView) findViewById(R.id.actvBrand);
itemACTV = (AutoCompleteTextView) findViewById(R.id.actvItemName);
partACTV = (AutoCompleteTextView) findViewById(R.id.actvPart);
barcodeACTV = (AutoCompleteTextView) findViewById(R.id.actvBarcode);

brandACTV.setOnClickListener(actvClicked);
itemACTV.setOnClickListener(actvClicked);
partACTV.setOnClickListener(actvClicked);
barcodeACTV.setOnClickListener(actvClicked);

并且onClickListener()的定义如下:

OnClickListener actvClicked = new OnClickListener(){

    @Override
    public void onClick(View view){

        Log.d("Hi", "onClickActivated");

        switch (view.getId()) {
            case R.id.actvBrand:
                Log.d("Hi", "Brand pressed");
                break;

            case R.id.actvItemName:
                Log.d("Hi", "Item name pressed");
                break;

            case R.id.actvPart:
                Log.d("Hi", "Part pressed");
                break;

            case R.id.actvBarcode:
                Log.d("Hi", "Barcode pressed");
                break;
        }
    }
};

但是,只能正确检测初始点击。所有后续点击仅在用户点击两次相同的actv时才会注册。

有关如何解决此问题的任何想法?谢谢。

3 个答案:

答案 0 :(得分:0)

brandACTV = (AutoCompleteTextView) findViewById(R.id.actvBrand);
itemACTV = (AutoCompleteTextView) findViewById(R.id.actvItemName);
partACTV = (AutoCompleteTextView) findViewById(R.id.actvPart);
barcodeACTV = (AutoCompleteTextView) findViewById(R.id.actvBarcode);

brandACTV.setOnClickListener(actvClicked);
itemACTV.setOnClickListener(actvClicked);
partACTV.setOnClickListener(actvClicked);
barcodeACTV.setOnClickListener(actvClicked);

并且onClickListener的定义如下:

OnClickListener actvClicked = new OnClickListener(){

    @Override
    public void onClick(View view){

        Log.d("Hi", "onClickActivated");

        switch (view.getId()) {
            case R.id.actvBrand:
            actvBrand.requestFocus();
                Log.d("Hi", "Brand pressed");
                break;

            case R.id.actvItemName:
             actvItemName.requestFocus();
                Log.d("Hi", "Item name pressed");
                break;

            case R.id.actvPart:
         actvPart.requestFocus();
                Log.d("Hi", "Part pressed");
                break;

            case R.id.actvBarcode:
                actvBarcode.requestFocus();
                Log.d("Hi", "Barcode pressed");
                break;
        }
    }
};

答案 1 :(得分:0)

我认为您必须从AutoCompleteTextView移除焦点: 转到您的XML并添加此属性android:clickable="true"android:focusable="false"

答案 2 :(得分:0)

改为使用onFocusChangedListener。像这样:

    brandACTV.setOnFocusChangeListener(focusChanged);
    itemACTV.setOnFocusChangeListener(focusChanged);
    partACTV.setOnFocusChangeListener(focusChanged);
    barcodeACTV.setOnFocusChangeListener(focusChanged);

然后将侦听器定义为:

OnFocusChangeListener focusChanged = new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {

        Log.d("Hi", "Id is " + v.getId() + ". Focus is " + hasFocus);

        // TODO Auto-generated method stub
        switch( v.getId() ){
            case R.id.actvBrand:
                Log.d("Hi", "Brand focus is + " + hasFocus);
                break;

            case R.id.actvItemName:
                Log.d("Hi", "ItemName focus is + " + hasFocus);
                break;

            case R.id.actvPart:
                Log.d("Hi", "Part focus is + " + hasFocus);
                break;

            case R.id.actvBarcode:
                Log.d("Hi", "Barcode focus is + " + hasFocus);
                break;

        }
    }
};