我有一个用于排序的菜单项。当用户单击该按钮时,将创建具有排序选项的PopupMenu。
现在我为每个项目创建了RadioButtons,但似乎无法将所选单选按钮设置为检查状态。我不知道出了什么问题。
这是我的menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="@+id/group"
android:checkableBehavior="single">
<item
android:id="@+id/fileName"
android:title="Name" />
<item
android:id="@+id/fileDate"
android:title="Date" />
</group>
</menu>
这就是我在onOptionsItemSelected()
中所拥有的if (id == R.id.sort) {
final PopupMenu popupMenu = new PopupMenu(getActivity(), view);
popupMenu.getMenuInflater().inflate(R.menu.sort_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if (id == R.id.fileName) {
sortOrder = 0;
} else if (id == R.id.fileDate) {
sortOrder = 1;
} else if (id == R.id.fileSizeInc) {
sortOrder = 2;
} else if (id == R.id.fileSizeDec) {
sortOrder = 3;
}
item.setChecked(!item.isChecked());
return true;
}
});
popupMenu.show();
}
任何人都可以帮我解决吗?
更新
我意识到@gfpacheco回答的错误。我需要在显示弹出菜单之后而不是之前执行此操作。但问题是如何才能获得特定的ClickItem,以便我可以在回调之外以编程方式检查它?
答案 0 :(得分:3)
首先,您应该有一个字段来保存当前的排序顺序,可能是默认值。
其次,在调用popupMenu.show()
之前,您应该设置相应的单选按钮选中状态:
MenuItem menuItem;
switch (sortOrder) {
case 0:
menuItem = popupMenu.getMenu().findItem(R.id.menu_item_0);
break;
case 1:
menuItem = popupMenu.getMenu().findItem(R.id.menu_item_1);
break;
case 2:
menuItem = popupMenu.getMenu().findItem(R.id.menu_item_2);
break;
}
menuItem.setChecked();
第三,在菜单点击回调中更新当前排序顺序的值:
sortOrder = newSortOrder;
这样当弹出窗口再次打开时,第二步将确保已经检查了当前的排序顺序。
答案 1 :(得分:-1)
这里是我找到的两个选项菜单项的解决方案,你想用菜单和group.working 100%
包装它将这两个项目添加到android:orderInCategory =&#34; 1&#34;对于第一项和android:orderInCategory =&#34; 2&#34;第二项
答案 2 :(得分:-1)
private static void DownloadLargeFile()
{
var account = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
var blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("container-name");
var file = "my-very-large-file-name";
var blob = container.GetBlockBlobReference(file);
//First fetch the size of the blob. We use this to create an empty file with size = blob's size
blob.FetchAttributes();
var blobSize = blob.Properties.Length;
long blockSize = (1 * 1024 * 1024);//1 MB chunk;
blockSize = Math.Min(blobSize, blockSize);
//Create an empty file of blob size
using (FileStream fs = new FileStream(file, FileMode.Create))//Create empty file.
{
fs.SetLength(blobSize);//Set its size
}
var blobRequestOptions = new BlobRequestOptions
{
RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(5), 3),
MaximumExecutionTime = TimeSpan.FromMinutes(60),
ServerTimeout = TimeSpan.FromMinutes(60)
};
long currentPointer = 0;
long bytesRemaining = blobSize;
do
{
var bytesToFetch = Math.Min(blockSize, bytesRemaining);
using (MemoryStream ms = new MemoryStream())
{
//Download range (by default 1 MB)
blob.DownloadRangeToStream(ms, currentPointer, bytesToFetch, null, blobRequestOptions);
ms.Position = 0;
var contents = ms.ToArray();
using (var fs = new FileStream(file, FileMode.Open))//Open that file
{
fs.Position = currentPointer;//Move the cursor to the end of file.
fs.Write(contents, 0, contents.Length);//Write the contents to the end of file.
}
currentPointer += contents.Length;//Update pointer
bytesRemaining -= contents.Length;//Update bytes to fetch
}
}
while (bytesRemaining > 0);
}