17

setOnFocusChangeListener を使用して SQLiteDatabase に保存したい EditText フィールドがいくつかあります。それぞれに onFocusChangeListener を個別に設定する必要がありますか、それとも何らかのキャッチオールがありますか? (これはフラグメントであるため、getActivity().findViewByID)

final TextView txtName = (TextView)getActivity().findViewById(R.id.clientHeader);
final TextView txtCompany = (TextView)getActivity().findViewById(R.id.txtContactCompany);       
final TextView txtPosition = (TextView)getActivity().findViewById(R.id.txtContactPosition);     


txtName.setOnFocusChangeListener(new OnFocusChangeListener() {          
    public void onFocusChange(View v, boolean hasFocus) {
        if(!hasFocus) {
            saveThisItem(txtClientID.getText().toString(), "name", txtName.getText().toString());
        }
    }
});


txtCompany.setOnFocusChangeListener(new OnFocusChangeListener() {          
    public void onFocusChange(View v, boolean hasFocus) {
        if(!hasFocus) {
            saveThisItem(txtClientID.getText().toString(), "company", txtCompany.getText().toString());
        }
    }
});

txtPosition.setOnFocusChangeListener(new OnFocusChangeListener() {          
    public void onFocusChange(View v, boolean hasFocus) {
        if(!hasFocus) {
            saveThisItem(txtClientID.getText().toString(), "position", txtPosition.getText().toString());
        }
    }
});

同様に... EditTextビューのArrayList < EditText >を取得し、既存のeditTextにポインターを割り当て(申し訳ありませんが、方法はわかりません)、onFocusChangeListenerをarraylist全体に設定する方法はありますか?それとも、ArrayList を繰り返し処理し、onFocusChangeListener を各メンバーに設定しますか?

または、任意の onFocusChangeListener イベントを検出し、イベントが発生した EditText に関係なく、すべてのデータをデータベースに保存する方法はありますか?

4

4 に答える 4

3

またはさらに単純な

OnFocusChangeListener listener;

listener = new new OnFocusChangeListener() {          
    public void onFocusChange(View v, boolean hasFocus) {
...        }
    }
});

textbox1. setOnFocusChangeListener(listener);
textbox2. setOnFocusChangeListener(listener);
textbox3. setOnFocusChangeListener(listener);

onFocusChange(View v, boolean hasFocus) にはパラメーター View があるため、それを使用して、どのビューがそれを呼び出したかを知ることができます

于 2013-05-01T02:13:18.053 に答える