Android AlertDialog 在上下文菜单中动态更改文本大小
在一项活动中,我有两个文本视图。在上下文菜单中,我可以选择更改其中一个文本视图的文本大小。我尝试了这样的事情......
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的应用程序崩溃是因为它尝试访问
items
数组中具有负索引的元素。发生这种情况是因为这些行:如果您仔细查看 DialogInterface.OnClickListener 文档,您会注意到它的
onClick()
方法接受诸如BUTTON_POSITIVE
、BUTTON_NEUTRAL
和BUTTON_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 you look carefully at DialogInterface.OnClickListener documentation you'll notice that its
onClick()
method accepts such constants asBUTTON_POSITIVE
,BUTTON_NEUTRAL
andBUTTON_NEGATIVE
which all are negative and not connected to the list items.