我正在尝试在自定义对话框上填充NumberPicker。目前,我正在使用一种使用View并使另一种布局(对话框)膨胀的方法,然后该方法应填充NumberPicker,但是当我在onCreate中调用它时,它不会。
该方法的代码:
public void fillArray() {
View inflatedView = getLayoutInflater().inflate(R.layout.add_activity, null);
np_hours = ( NumberPicker ) inflatedView.findViewById( R.id.hourNumber );
np_minutes = ( NumberPicker ) inflatedView.findViewById( R.id.minuteNumber );
String[] hoursArray = new String[25];
String[] minutesArray = new String[61];
for( i = 0; i < hoursArray.length; i++ ) {
hoursArray[i] = Integer.toString( i );
}
for( x = 0 ; x < minutesArray.length; x++ ){
minutesArray[x] = Integer.toString( x );
}
np_hours.setMinValue( 0 );
np_hours.setMaxValue( 24 );
np_hours.setWrapSelectorWheel( false );
np_hours.setDisplayedValues( hoursArray );
np_minutes.setMinValue( 0 );
np_minutes.setMaxValue( 60 );
np_minutes.setWrapSelectorWheel( false );
np_minutes.setDisplayedValues( minutesArray );
}
编辑:
这样的想法是选择某件事的持续时间(而不是时间)-如果造成混乱,请您谅解!
由于只需要连续的数字,因此无需创建边数组。
您可以按照以下步骤进行操作:
public void fillArray() {
View inflatedView = getLayoutInflater().inflate(R.layout.add_activity, null);
np_hours = ( NumberPicker ) inflatedView.findViewById( R.id.hourNumber );
np_hours.setMaxValue(24);
np_hours.setMinValue(0);
np_minutes = ( NumberPicker ) inflatedView.findViewById( R.id.minuteNumber );
np_minutes.setMaxValue(60);
np_minutes.setMinValue(0);
}
如果这不起作用,请发布您的XML布局和完整的活动代码(至少是onCreate()方法)。
参考:
更新资料
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.plan_activity );
}
@Override
public boolean onCreateOptionsMenu( Menu menu ) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate( R.menu.main, menu );
return true;
}
@SuppressLint("NewApi")
public void adActivity( View view ) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View parentView = inflater.inflate(R.layout.add_activity, null);
LayoutInflater inflater = this.getLayoutInflater();
builder.setView(parentView);
builder.setPositiveButton(R.string.add_activity, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Stert
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
fillArray(parentView);
// Create the AlertDialog object and return it
AlertDialog mainAlert = builder.create();
mainAlert.show();
}
// Method to fill the NumberPicker's
public void fillArray(View view) {
View inflatedView = view;
np_hours = ( NumberPicker ) inflatedView.findViewById( R.id.hourNumber );
np_hours.setMaxValue(24);
np_hours.setMinValue(0);
np_minutes = ( NumberPicker ) inflatedView.findViewById( R.id.minuteNumber );
np_minutes.setMaxValue(60);
np_minutes.setMinValue(0);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句