package com.example.dentalproject;
import android.app.Activity;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener
{
/** Called when the activity is first created. */
EditText ettokenno,etcomments;
Button btnfeedback;
Spinner ratingspinner;
DatabaseAdapter dbAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbAdapter=new DatabaseAdapter(getApplicationContext());
ettokenno=(EditText)findViewById(R.id.ed_tokennum);
etcomments=(EditText)findViewById(R.id.ed_yourcomments);
ratingspinner = (Spinner)findViewById(R.id.sp1_select);
btnfeedback=(Button)findViewById(R.id.btnfeedback);
btnfeedback.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.btnfeedback)
//if(ettokenno.equals("")&&(etcomments.equals("")))
{
//Toast.makeText(getApplicationContext(), "save",Toast.LENGTH_LONG).show();
String Tokenno=ettokenno.getText().toString();
String Comments=etcomments.getText().toString();
String Rating = ratingspinner.getSelectedItem().toString();
//String Rating = "Good";
if(Tokenno.equals(""))
{
Toast.makeText(getApplicationContext(), "please enter value",Toast.LENGTH_LONG).show();
}
if(Comments.equals(""))
{
Toast.makeText(getApplicationContext(), "please enter value",Toast.LENGTH_LONG).show();
}
dbAdapter.open();
long inserted=dbAdapter.insertTest(Tokenno,Comments, Rating);
if(inserted >0)
{
Toast.makeText(getApplicationContext(), "data saved",Toast.LENGTH_LONG).show();
ettokenno.setText("");
etcomments.setText("");
}
else
{
Toast.makeText(getApplicationContext(), "data not saved",Toast.LENGTH_LONG).show();
}
dbAdapter.close();
}
在这个应用程序中我有两个edittexts和一个微调器,如何限制用户填写所有字段以及当用户离开编辑文本和微调器没有填充数据时如何生成对话框,请提前帮助我的朋友 } }
答案 0 :(得分:0)
使用布尔变量,比如flag
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.btnfeedback)
//if(ettokenno.equals("")&&(etcomments.equals("")))
{
//Toast.makeText(getApplicationContext(), "save",Toast.LENGTH_LONG).show();
String Tokenno=ettokenno.getText().toString();
String Comments=etcomments.getText().toString();
String Rating = ratingspinner.getSelectedItem().toString();
boolean flag = true;
if(Tokenno.equals(""))
{
Toast.makeText(getApplicationContext(), "please enter value",Toast.LENGTH_LONG).show();
flag = false;
}
if(Comments.equals(""))
{
Toast.makeText(getApplicationContext(), "please enter value",Toast.LENGTH_LONG).show();
flag = false;
}
if(flag) {
dbAdapter.open();
long inserted=dbAdapter.insertTest(Tokenno,Comments, Rating);
if(inserted >0)
{
Toast.makeText(getApplicationContext(), "data saved",Toast.LENGTH_LONG).show();
ettokenno.setText("");
etcomments.setText("");
}
else
{
Toast.makeText(getApplicationContext(), "data not saved",Toast.LENGTH_LONG).show();
}
}
dbAdapter.close();
}
答案 1 :(得分:0)
在onClick中尝试以下代码
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.btnfeedback)
{
String Tokenno=ettokenno.getText().toString().trim();
if(TextUtils.isEmpty(Tokenno)){
//show dialog for Tokenno
return;
}
String Comments=etcomments.getText().toString();
if(TextUtils.isEmpty(Comments)){
//show dialog for Comments
return;
}
String Rating = ratingspinner.getSelectedItem().toString();
if(Rating.equals("YOUR_DEFAULT_VALUE")){
//show dialog for rating
return;
}
//all fields are filled
}
以下Tutorial显示不同类型的对话框
答案 2 :(得分:0)
这适用于两个TextView
if (!text1.getText().toString().isEmpty() && !text2.getText().toString().isEmpty()){
// Your Dialog or Toast for warning
}
else{
// your further code
}
对于Spinner
,您可以设置setOnItemSelectedListener
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinner.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
spinner.setSelection(0);
}
});