温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

GridView+PopupWindow自定义数字键盘

发布时间:2020-09-03 08:59:06 来源:网络 阅读:666 作者:二零一八 栏目:移动开发

public class SimpleGridViewTest extends Activity {
EditText editText;
private PopupWindow popupWindow;
private GridView mGridView;
private View contentview;
private List<String> datas = new ArrayList<String>();
GridViewAdapter adapter;
private static String editNum = "";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);
    editText = (EditText) findViewById(R.id.editText);
    initData();
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    contentview = inflater.inflate(R.layout.simple_grid_view_test, null);
    popupWindow = new PopupWindow(contentview, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setFocusable(true);
    popupWindow.setOutsideTouchable(false);
    mGridView = (GridView) contentview.findViewById(R.id.gridview);
    adapter = new GridViewAdapter(this, R.layout.grid_view_item, datas);
    mGridView.setAdapter(adapter);
    mGridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String s = datas.get(position);
            Toast.makeText(SimpleGridViewTest.this, "xuan" + position, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
    mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String num = editText.getText().toString().trim();
            editNum = datas.get(position);
            if (editNum.equals("OK")) {
                editText.setText(editText.getText().toString().trim());
                popupWindow.dismiss();
            } else if (editNum.equals("")) {
                if (num.length() > 0) {
                    editText.setText(num.substring(0, num.length() - 1));
                    editText.setSelection(editText.getText().length());
                }
            } else {
                editText.setText(editText.getText().toString().trim() + datas.get(position));
                editText.setSelection(editText.getText().length());
            }

// Toast.makeText(SimpleGridViewTest.this,"dian"+position,Toast.LENGTH_SHORT).show();
}
});

    // 设置不调用系统键盘
    if (Build.VERSION.SDK_INT <= 10) {
        editText.setInputType(InputType.TYPE_NULL);
    } else {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        try {
            Class<EditText> cls = EditText.class;
            Method setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
            setShowSoftInputOnFocus.setAccessible(true);
            setShowSoftInputOnFocus.invoke(editText, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    editText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popupWindow.showAsDropDown(editText);
        }
    });
}
@Override
public void onBackPressed() {       //点击返回按钮
    if (popupWindow.isShowing()) {
        popupWindow.dismiss();
        return;
    }
    super.onBackPressed();
}
// 填充数据
private void initData() {
    for (int i = 0; i < 12; i++) {
        if (i < 3) {
            datas.add(String.valueOf(i + 1));
        } else if (i > 3 && i < 7) {
            datas.add(String.valueOf(i));
        } else if (i > 7 && i < 11) {
            datas.add(String.valueOf(i - 1));
        } else if (i == 3) {
            datas.add("OK");
        } else if (i == 11) {
            datas.add("0");
        } else {
            datas.add("");
        }
    }
}

}

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI