您好我正在为我的应用程序实现一个计算器类型键盘,用户点击它想要在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
}
}
所以请帮我解决这个问题..........
答案 0 :(得分: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
}
});
同样调用keypadAdaptor.setOnButtonClickListener(onClickListener.....);
不会做任何事情,因为您在keypadAdator中的方法定义是空的。