0

嗨,我正在为我的应用程序实现一个计算器类型的键盘,用户单击它想要在 Edittext 中显示的键盘,但我没有收到任何错误,也没有结果这是我的 java 代码

public class MainActivity extends Activity {
GridView mKeypadGrid;
KeypadAdapter mKeypadAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Get reference to the keypad button GridView
    mKeypadGrid = (GridView) findViewById(R.id.grdButtons);

    // Create Keypad Adapter
    mKeypadAdapter = new KeypadAdapter(this);

    // Set adapter of the keypad grid
    mKeypadGrid.setAdapter(mKeypadAdapter);

    // Set button click listener of the keypad adapter
    mKeypadAdapter.setOnButtonClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Button btn = (Button) v;
            // Get the KeypadButton value which is used to identify the
            // keypad button from the Button's tag
            KeypadButton keypadButton = (KeypadButton) btn.getTag();

            // Process keypad button
            ProcessKeypadInput(keypadButton);
        }
    });

    mKeypadGrid.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

        }
    });

}

protected void ProcessKeypadInput(KeypadButton keypadButton) {
    // TODO Auto-generated method stub
    Toast.makeText(
            MainActivity.this,
            keypadButton.getText().toString() + " "
                    + keypadButton.toString(), Toast.LENGTH_SHORT).show();


        }
      }

这是我的 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
  }
  }

所以请帮我解决这个问题............

4

1 回答 1

0

据我所知,要使 gridView 中的项目可点击,您需要完全实现 onItemClickListener 并使用 onClickListener 实际将该侦听器分配给网格视图,而不是网格视图中的每个单独按钮。我认为,如果您将 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
    } 
});

由于您在 keypadAdator 中的方法定义为空,因此调用keypadAdaptor.setOnButtonClickListener(onClickListener.....);也不会做任何事情。

于 2013-03-08T10:03:01.960 回答