📄 choicegroup.java
字号:
* @param stringPart the string part of the new element
* @param imagePart the image part of the element, or null if there is no image part
* @param elementStyle the style for the new list element.
* @throws IndexOutOfBoundsException if elementNum is invalid
* @throws NullPointerException if stringPart is null
* @see Choice#set(int, String, Image) in interface Choice
*/
public void set(int elementNum, String stringPart, Image imagePart, Style elementStyle )
{
ChoiceItem item = (ChoiceItem) this.itemsList.get( elementNum );
item.setText( stringPart );
if (imagePart != null) {
item.setImage(imagePart);
}
if (elementStyle != null) {
item.setStyle(elementStyle);
}
//#if polish.i18n.useDynamicTranslations
//# // if (elementNum == this.focusedIndex) {
//# // if (this.selectCommand == List.SELECT_COMMAND ) {
//# // String selectLabel = Locale.get("polish.command.select");
//# // if ( selectLabel != List.SELECT_COMMAND.getLabel()) {
//# // List.SELECT_COMMAND = new Command( selectLabel, Command.ITEM, 3 );
//# // removeCommand(this.selectCommand);
//# // this.selectCommand = List.SELECT_COMMAND;
//# // addCommand( this.selectCommand );
//# // }
//# // }
//# // }
//#endif
if (this.isInitialized) {
this.isInitialized = false;
repaint();
}
}
/**
* Sets the <code>ChoiceItem</code> of the
* element referenced by <code>elementNum</code>,
* replacing the previous one.
*
* @param elementNum the index of the element to be set
* @param item the ChoiceItem of the new element
* @throws IndexOutOfBoundsException if elementNum is invalid
*/
public void set(int elementNum, ChoiceItem item )
{
delete( elementNum );
add( elementNum, item );
if (this.isInitialized) {
this.isInitialized = false;
repaint();
}
}
/**
* Deletes the element referenced by <code>elementNum</code>.
*
* @param elementNum the index of the element to be deleted
* @throws IndexOutOfBoundsException if elementNum is invalid
* @see Choice#delete(int) in interface Choice
*/
public void delete(int elementNum)
{
remove(elementNum);
//#ifdef polish.usePopupItem
//# if (this.isPopup) {
//# if (this.selectedIndex == elementNum ) {
//# if (this.itemsList.size() > 0) {
//# this.selectedIndex = -1;
//# if (this.isPopupClosed) {
//# setSelectedIndex( 0, true );
//# }
//# } else {
//# this.selectedIndex = -1;
//# }
//# } else if ( elementNum < this.selectedIndex ) {
//# this.selectedIndex--;
//# }
//# } else {
//#endif
if (this.selectedIndex == elementNum ) {
this.selectedIndex = -1;
} else if (elementNum < this.selectedIndex) {
this.selectedIndex--;
}
//#ifdef polish.usePopupItem
//# }
//#endif
}
/**
* Deletes all elements from this <code>ChoiceGroup</code>.
*
* @see Choice#deleteAll() in interface Choice
*/
public void deleteAll()
{
clear();
this.selectedIndex = -1;
}
/**
* Gets a boolean value indicating whether this element is selected.
*
* @param elementNum the index of the element to be queried
* @return selection state of the element
* @throws IndexOutOfBoundsException if elementNum is invalid
* @see Choice#isSelected(int) in interface Choice
*/
public boolean isSelected(int elementNum)
{
ChoiceItem item = (ChoiceItem) this.itemsList.get( elementNum );
return item.isSelected;
}
/**
* Returns the index number of an element in the
* <code>ChoiceGroup</code> that is
* selected. For <code>ChoiceGroup</code> objects of type
* <code>EXCLUSIVE</code> and <code>POPUP</code>
* there is at most one element selected, so
* this method is useful for determining the user's choice.
* Returns <code>-1</code> if
* there are no elements in the <code>ChoiceGroup</code>.
*
* <p>For <code>ChoiceGroup</code> objects of type
* <code>MULTIPLE</code>, this always
* returns <code>-1</code> because no
* single value can in general represent the state of such a
* <code>ChoiceGroup</code>.
* To get the complete state of a <code>MULTIPLE</code>
* <code>Choice</code>, see <A HREF="../../../javax/microedition/lcdui/ChoiceGroup.html#getSelectedFlags(boolean[])"><CODE>getSelectedFlags</CODE></A>.</p>
*
* @return index of selected element, or -1 if none
* @see Choice#getSelectedIndex() in interface Choice
* @see #setSelectedIndex(int, boolean)
*/
public int getSelectedIndex()
{
if (this.isMultiple || this.itemsList.size() == 0) {
return -1;
} else if (this.isImplicit) {
return this.focusedIndex;
} else {
return this.selectedIndex;
}
}
/**
* Queries the state of a <code>ChoiceGroup</code> and returns the state of
* all elements in the
* boolean array
* <code>selectedArray_return</code>. <strong>Note:</strong> this
* is a result parameter.
* It must be at least as long as the size
* of the <code>ChoiceGroup</code> as returned by <code>size()</code>.
* If the array is longer, the extra
* elements are set to <code>false</code>.
*
* <p>For <code>ChoiceGroup</code> objects of type
* <code>MULTIPLE</code>, any
* number of elements may be selected and set to true in the result
* array. For <code>ChoiceGroup</code> objects of type
* <code>EXCLUSIVE</code> and <code>POPUP</code>
* exactly one element will be selected, unless there are
* zero elements in the <code>ChoiceGroup</code>. </p>
*
* @param selectedArray_return array to contain the results
* @return the number of selected elements in the ChoiceGroup
* @throws IllegalArgumentException if selectedArray_return is shorter than the size of the ChoiceGroup
* @throws NullPointerException if selectedArray_return is null
* @see Choice#getSelectedFlags(boolean[]) in interface Choice
* @see #setSelectedFlags(boolean[])
*/
public int getSelectedFlags(boolean[] selectedArray_return)
{
//#ifndef polish.skipArgumentCheck
if (selectedArray_return.length < this.itemsList.size()) {
//#ifdef polish.verboseDebug
//# throw new IllegalArgumentException("length of selectedArray is too small");
//#else
throw new IllegalArgumentException();
//#endif
}
//#endif
ChoiceItem[] myItems = (ChoiceItem[]) this.itemsList.toArray( new ChoiceItem[ this.itemsList.size() ] );
int selectedItems = 0;
for (int i = 0; i < myItems.length; i++) {
ChoiceItem item = myItems[i];
if (item.isSelected || (this.isImplicit && i == this.focusedIndex) ) {
selectedArray_return[i] = true;
selectedItems++;
} else {
selectedArray_return[i] = false;
}
}
return selectedItems;
}
/**
* For <code>ChoiceGroup</code> objects of type
* <code>MULTIPLE</code>, this simply sets an
* individual element's selected state.
*
* <P>For <code>ChoiceGroup</code> objects of type
* <code>EXCLUSIVE</code> and <code>POPUP</code>, this can be used only to
* select an element. That is, the <code> selected </code> parameter must
* be <code> true </code>. When an element is selected, the previously
* selected element is deselected. If <code> selected </code> is <code>
* false </code>, this call is ignored.</P>
*
* <p>For both list types, the <code>elementNum</code> parameter
* must be within
* the range
* <code>[0..size()-1]</code>, inclusive. </p>
*
* @param elementNum the number of the element. Indexing of the elements is zero-based
* @param selected the new state of the element true=selected, false=not selected
* @throws IndexOutOfBoundsException if elementNum is invalid
* @see Choice#setSelectedIndex(int, boolean) in interface Choice
* @see #getSelectedIndex()
*/
public void setSelectedIndex(int elementNum, boolean selected)
{
if (this.isMultiple) {
ChoiceItem item = (ChoiceItem) this.itemsList.get( elementNum );
item.select( selected );
} else {
if (selected == false) {
return; // ignore this call
}
if (this.selectedIndex != -1) {
ChoiceItem oldSelected = (ChoiceItem) this.itemsList.get( this.selectedIndex );
oldSelected.select( false );
}
ChoiceItem newSelected = (ChoiceItem) this.itemsList.get( elementNum );
newSelected.select( true );
this.selectedIndex = elementNum;
if (this.isFocused) {
if ( this.isInitialized) {
focus( elementNum, newSelected, 0 );
} else {
this.autoFocusEnabled = true;
this.autoFocusIndex = elementNum;
}
}
//#ifdef polish.usePopupItem
//# if (this.isPopup) {
//# this.popupItem.setText( newSelected.getText() );
//# }
//#endif
}
if (this.isInitialized) {
this.isInitialized = false;
repaint();
}
}
/**
* Attempts to set the selected state of every element in the
* <code>ChoiceGroup</code>. The array
* must be at least as long as the size of the
* <code>ChoiceGroup</code>. If the array is
* longer, the additional values are ignored. <p>
*
* For <code>ChoiceGroup</code> objects of type
* <code>MULTIPLE</code>, this sets the selected
* state of every
* element in the <code>Choice</code>. An arbitrary number of
* elements may be selected.
* <p>
*
* For <code>ChoiceGroup</code> objects of type
* <code>EXCLUSIVE</code> and <code>POPUP</code>, exactly one array
* element must have the value <code>true</code>. If no element is
* <code>true</code>,
* the first element
* in the <code>Choice</code> will be selected. If two or more
* elements are <code>true</code>, the
* implementation will choose the first <code>true</code> element
* and select it. <p>
*
* @param selectedArray an array in which the method collect the selection status
* @throws IllegalArgumentException if selectedArray is shorter than the size of the ChoiceGroup
* @throws NullPointerException if the selectedArray is null
* @see Choice#setSelectedFlags(boolean[]) in interface Choice
* @see #getSelectedFlags(boolean[])
*/
public void setSelectedFlags(boolean[] selectedArray)
{
if (selectedArray == null || selectedArray.length == 0) {
// ignore these flags
return;
}
//#ifndef polish.skipArgumentCheck
if (selectedArray.length < this.itemsList.size()) {
//#ifdef polish.verboseDebug
//# throw new IllegalArgumentException("length of selectedArray is too small");
//#else
throw new IllegalArgumentException();
//#endif
}
//#endif
if (this.isMultiple) {
ChoiceItem[] myItems = (ChoiceItem[]) this.itemsList.toArray( new ChoiceItem[ this.itemsList.size() ] );
for (int i = 0; i < myItems.length; i++) {
ChoiceItem item = myItems[i];
item.select( selectedArray[i]);
}
} else {
int index = 0;
for (int i = 0; i < selectedArray.length; i++) {
if (selectedArray[i]) {
index = i;
break;
}
}
if (index > this.itemsList.size()) {
index = 0;
}
setSelectedIndex( index, true );
}
if (this.isInitialized) {
this.isInitialized = false;
repaint();
}
}
/**
* Sets the application's preferred policy for fitting
* <code>Choice</code> element contents to the available screen space. The set policy applies for all
* elements of the <code>Choice</code> object. Valid values are
* <CODE>Choice.TEXT_WRAP_DEFAULT</CODE>,
* <CODE>Choice.TEXT_WRAP_ON</CODE>,
* and <CODE>Choice.TEXT_WRAP_OFF</CODE>.
* Fit policy is a hint, and the
* implementation may disregard the application's preferred policy.
* The J2ME Polish implementation always uses the TEXT_WRAP_ON policy.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -