Android AlertDialog 在上下文菜单中动态更改文本大小

发布于 2024-12-05 01:56:34 字数 1926 浏览 2 评论 0原文

在一项活动中,我有两个文本视图。在上下文菜单中,我可以选择更改其中一个文本视图的文本大小。我尝试了这样的事情......

       public boolean onOptionsItemSelected(MenuItem item){
          switch (item.getItemId()){
                  case R.id.menutextSize:
                    final CharSequence[] items = {"Normal","Large","Larger"};
                    AlertDialog.Builder builder = new      

           AlertDialog.Builder(this);
                    builder.setTitle("Select TextSize");
                    builder.setSingleChoiceItems(items, -1, 
                            new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {
                            Toast.makeText(getApplicationContext(), items[item],
                                    Toast.LENGTH_SHORT).show();
                        }
                    });

                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {

                            int textSize = (int)mBodyText.getTextSize();  
                            if (items[whichButton] == "Normal")    
                            {
                                mTextv.setTextSize(12);
                            }
                            if (items[whichButton] == "Large")    
                            {
                                mTextv.setTextSize(14);
                            }
                            if (items[whichButton] == "Larger")    
                            {
                                mTextv.setTextSize(16);
                            }




                        }
                    });
                    builder.setNegativeButton("cancel", null);
                    builder.show();
                    return true;    
          }

当我点击单选按钮时,它显示“强制关闭”消息。我该如何解决这个问题? 谢谢..

In an activity, I have two Text Views. In context menu, I have an options to change text size of one of the text view. I tried something like this..

       public boolean onOptionsItemSelected(MenuItem item){
          switch (item.getItemId()){
                  case R.id.menutextSize:
                    final CharSequence[] items = {"Normal","Large","Larger"};
                    AlertDialog.Builder builder = new      

           AlertDialog.Builder(this);
                    builder.setTitle("Select TextSize");
                    builder.setSingleChoiceItems(items, -1, 
                            new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {
                            Toast.makeText(getApplicationContext(), items[item],
                                    Toast.LENGTH_SHORT).show();
                        }
                    });

                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {

                            int textSize = (int)mBodyText.getTextSize();  
                            if (items[whichButton] == "Normal")    
                            {
                                mTextv.setTextSize(12);
                            }
                            if (items[whichButton] == "Large")    
                            {
                                mTextv.setTextSize(14);
                            }
                            if (items[whichButton] == "Larger")    
                            {
                                mTextv.setTextSize(16);
                            }




                        }
                    });
                    builder.setNegativeButton("cancel", null);
                    builder.show();
                    return true;    
          }

t when I am clciking in the radiobutton it is showing "Force close " messsage. How can I solve this?
Thank you..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落花浅忆 2024-12-12 01:56:34

您的应用程序崩溃是因为它尝试访问 items 数组中具有负索引的元素。发生这种情况是因为这些行:

if (items[whichButton] == "...")

如果您仔细查看 DialogInterface.OnClickListener 文档,您会注意到它的 onClick() 方法接受诸如 BUTTON_POSITIVEBUTTON_NEUTRALBUTTON_NEGATIVE 均为负数且与列表项无关。

Your app crashes because it tries to access an element with a negative index in the items array. It happens because of these lines:

if (items[whichButton] == "...")

If you look carefully at DialogInterface.OnClickListener documentation you'll notice that its onClick() method accepts such constants as BUTTON_POSITIVE, BUTTON_NEUTRAL and BUTTON_NEGATIVE which all are negative and not connected to the list items.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文