我是android studio的新手,并创建了一个带有页面的应用程序,该页面可以选择2个微调器,用户输入一个时间然后结束时间'计算。我正在尝试为此页面添加一些验证,因为当我点击“验证按钮”时应用程序崩溃,因为没有选择任何字段。我的意图是当用户选择“验证按钮”时。显示错误消息以确保填充微调器值,这意味着应用程序不会崩溃。我在onNothingSelected中尝试了一些方法,但它似乎没有用。我不确定它是否需要在那里或VerifyButton?提前谢谢!
Spinner Java
final ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(CreateLine.this,
R.layout.spinner_layout, getResources().getStringArray(R.array.LineTypes));
myAdapter.setDropDownViewResource(R.layout.spinner_layout);
spinner.setAdapter(myAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position2, long l) {
switch (position2) {
case 0:
quantity.setText("");
break;
case 1:
quantity.setVisibility(View.INVISIBLE);
timeadded = 10;
duration.setText(timeadded + " Hours");
spinner2.setVisibility(View.INVISIBLE);
break;
case 2:
quantity.setVisibility(View.INVISIBLE);
timeadded = 10;
duration.setText(timeadded + " Hours");
spinner2.setVisibility(View.INVISIBLE);
break;
case 3:
quantity.setVisibility(View.INVISIBLE);
timeadded = 10;
duration.setText(timeadded + " Hours");
spinner2.setVisibility(View.INVISIBLE);
break;
case 4:
quantity.setVisibility(View.VISIBLE);
duration.setText(timeadded + " Hours");
spinner2.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "Select Package Type", Toast.LENGTH_SHORT).show();
break;
case 5:
quantity.setVisibility(View.VISIBLE);
duration.setText(timeadded + " Hours");
spinner2.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "Select Package Type", Toast.LENGTH_SHORT).show();
break;
case 6:
quantity.setVisibility(View.VISIBLE);
duration.setText(timeadded + " Hours");
spinner2.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "Select Package Type", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
ArrayAdapter<String> myAdapter2 = new ArrayAdapter<String>(CreateLine.this,
R.layout.spinner_layout, getResources().getStringArray(R.array.PackageTypes));
myAdapter2.setDropDownViewResource(R.layout.spinner_layout);
spinner2.setAdapter(myAdapter2);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
switch (position) {
case 0:
quantity.setText("");
break;
case 1:
quantity.setText(PackageType20);
timeadded = 28;
duration.setText(timeadded + " Hours");
break;
case 2:
quantity.setText(PackageType30);
timeadded = 27;
duration.setText(timeadded + " Hours");
break;
default:
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
VerifyButton Java
VerifyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
spinnerSelection = String.valueOf(spinner.getSelectedItem());
spinnerSelection2 = String.valueOf(spinner2.getSelectedItem());
String q = quantity.getText().toString();
String d = duration.getText().toString();
DateTime datetime = new DateTime(yearNow, monthNow, dayNow, hourNow, minuteNow);
DateTimeFormatter fmt = DateTimeFormat.forPattern("EE dd MM yyyy" + "\n" + " h:mm a ");
String formattedtime = fmt.print(datetime);
CalculateButton.setText(formattedtime);
// Plus some hours, minutes, and seconds to the original DateTime.
DateTimeFormatter fmt2 = DateTimeFormat.forPattern("EE dd MM yyyy" + "\n" + " h:mm a ");
DateTime dateTime1 = datetime.plusHours(timeadded);
String endtimecalc = fmt2.print(dateTime1);
TextView endtime = findViewById(endtimetextView);
endtime.setVisibility(View.VISIBLE);
endtime.setText(endtimecalc);
if (spinnerSelection !=null) {
Toast.makeText(getApplicationContext(),"enter value",
Toast.LENGTH_LONG).show();
}
//INSERT DATA TO DATABASE
boolean isInserted = myDb.insertData(
spinnerSelection,
spinnerSelection2,
q,
d,
formattedtime,
endtimecalc);
if (isInserted == true)
Toast.makeText(CreateLine.this, "Data Inserted Successfully", Toast.LENGTH_LONG).show();
else
Toast.makeText(CreateLine.this, "This line is already Active!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), Dashboard.class);
startActivity(intent);
}
});
答案 0 :(得分:0)
在spinner.setAdapter(myAdapter)之后添加;
spinner.setAdapter(myAdapter)
spinner.setSelection(-1);
然后点击验证按钮:
if (spinner.getSelectedItemPosition() == -1 || spinner2.getSelectedItemPosition() == -1 ){
// showErrorMessage
}
else{
//do your work
}
对spinner2执行相同的操作
答案 1 :(得分:0)
替换所有你的:
$('#b1').on('click',function(){
alert("button #1 is clicked");
$('#b2').click();
});
$('#b2').on('click',function(){
alert("button #2 is clicked");
});
使用:
getApplicationContext()
并在以下行而不是getApplicationContext():
getContext()
使用您的班级名称,如下所示:
Intent intent = new Intent(getApplicationContext(), Dashboard.class);
您定义验证按钮的类。
在onNothingSelected方法中,将spinner的默认值设置为:
Intent intent = new Intent(YourClassName.this, Dashboard.class);
同时应用支票而不是:
ArrayList<String> list = getResources().getStringArray(R.array.LineTypes);
yourSpinner.setSelection(list[0]);
如:
if (spinnerSelection !=null) {
Toast.makeText(getApplicationContext(),"enter value",
Toast.LENGTH_LONG).show();
}