2013-03-08 91 views
0

你好我實現一個計算器式鍵盤我的應用程序在鍵盤上用戶點擊它希望在EDITTEXT顯示,但我沒有得到任何錯誤,也沒有結果這裏是我的Java代碼Android計算器鍵盤按鈕OnClickListener無法正常工作?

​​

這裏是我KeypadAdapter.java

public class KeypadAdapter extends BaseAdapter { 
private Context mContext; 

public KeypadAdapter(Context c) { 
    mContext = c; 
} 

public int getCount() { 
    return mButtons.length; 
} 

public Object getItem(int position) { 
    return mButtons[position]; 
} 

public long getItemId(int position) { 
    return 0; 
} 

// create a new ButtonView for each item referenced by the Adapter 
public View getView(int position, View convertView, ViewGroup parent) { 
    Button btn; 
    if (convertView == null) { // if it's not recycled, initialize some attributes 
     btn = new Button(mContext); 
     KeypadButton keypadButton = mButtons[position]; 

     // Set CalculatorButton enumeration as tag of the button so that we 
     // will use this information from our main view to identify what to do 
     btn.setTag(keypadButton); 
    } 
    else { 
     btn = (Button) convertView; 
    } 

    btn.setText(mButtons[position].getText()); 
    return btn; 
} 

// Create and populate keypad buttons array with CalculatorButton values 
private KeypadButton[] mButtons = {KeypadButton.SEVEN,KeypadButton.EIGHT, KeypadButton.NINE, 
     KeypadButton.FOUR, KeypadButton.FIVE,KeypadButton.SIX, 
     KeypadButton.ONE, KeypadButton.TWO, KeypadButton.THREE, 
     KeypadButton.ZERO,KeypadButton.DOT,KeypadButton.BACKSPACE }; 

public void setOnButtonClickListener(OnClickListener onClickListener) { 
    // TODO Auto-generated method stub 

} 
public enum KeypadButton { 
    BACKSPACE("Clear",KeypadButtonCategory.CLEAR) 
    , ZERO("0",KeypadButtonCategory.NUMBER) 
    , ONE("1",KeypadButtonCategory.NUMBER) 
    , TWO("2",KeypadButtonCategory.NUMBER) 
    , THREE("3",KeypadButtonCategory.NUMBER) 
    , FOUR("4",KeypadButtonCategory.NUMBER) 
    , FIVE("5",KeypadButtonCategory.NUMBER) 
    , SIX("6",KeypadButtonCategory.NUMBER) 
    , SEVEN("7",KeypadButtonCategory.NUMBER) 
    , EIGHT("8",KeypadButtonCategory.NUMBER) 
    , NINE("9",KeypadButtonCategory.NUMBER) 
    , DOT(".",KeypadButtonCategory.OTHER); 

    CharSequence mText; // Display Text 
    KeypadButtonCategory mCategory; 

    KeypadButton(CharSequence text,KeypadButtonCategory category) { 
     mText = text; 
     mCategory = category; 
    } 

    public CharSequence getText() { 
     return mText; 
    } 
} 
public enum KeypadButtonCategory { 
    MEMORYBUFFER 
    , NUMBER 
    , OPERATOR 
    , DUMMY 
    , CLEAR 
    , RESULT 
    , OTHER 
    } 
    } 

所以請幫我解決這個問題..........

回答

0

據我所知,讓你需要充分落實一個GridView點擊裏面的物品一個onItemClickListener並使用onClickL按照你的方式,實際上是將該監聽器分配給網格視圖,而不是將網格視圖中的每個單獨按鈕分配給網格視圖。我想如果你把你的onClickListener中的代碼放到你的onItemClickListener中,那麼它應該可以工作。

mKeypadAdapter.setOnButtonClickListener(new OnClickListener() { //This won't work 
    //because keypadAdaptor.setOnButtonClickAdaptor(.....); is empty 
    @Override public void onClick(View v) { 
     Button btn = (Button) v; 
     KeypadButton keypadButton = (KeypadButton) btn.getTag(); 
     ProcessKeypadInput(keypadButton); 
    } 
}); 

mKeypadGrid.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
    { 
    //This needs to be filled in with your button processing code 
    } 
}); 

還呼籲keypadAdaptor.setOnButtonClickListener(onClickListener.....);是不會在你的keypadAdator爲它做任何事情,因爲你的方法定義是空的。

+0

您好@chefburns你能詳細解答與代碼.... thanq – androidgeek 2013-03-08 10:07:44

+0

好吧,所以我想要實現,而不是...... – androidgeek 2013-03-08 10:25:42

+0

如果你把你的OnClickListener的代碼,並把它放在你的OnItemClickListener那麼它應該工作,你可以刪除mKeyAdapter.setOnButtonClickAdapter,因爲它沒有被使用。 – chefburns 2013-03-08 10:31:51