2014-05-05 38 views
-1

我对android编程比较陌生。目前使用Eclipse和我有一个问题。根据一定条件禁用和启用按钮

我被要求编写一个名为clockin的按钮,该按钮将检查“允许非工作站点”是否在时钟中。这意味着打开这个应用程序时,这个按钮会自动检查它是否允许非工作站点。如果允许,它将进行身份验证,如果不允许,它将检查其工作地点和当前位置之间的距离。如果在范围内,则启用按钮,如果不禁用该按钮。

这听起来可能相当复杂,但我的任务是尽快完成此任务。

我的代码如下:

public class MainActivity extends Activity { 

EditText txtusername; 
EditText txtpassword; 
Button btnclockin; 
Button btnclockout; 
GPSTracker gps; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    txtusername=(EditText)this.findViewById(R.id.username); 
    txtpassword=(EditText)this.findViewById(R.id.password); 
    btnclockin=(Button)this.findViewById(R.id.clockin); 
    btnclockout=(Button)this.findViewById(R.id.clockout); 
    btnclockin.setOnClickListener(new OnClickListener() { 

public void onClick(View v) { 
// TODO Auto-generated method stub 

if((txtusername.getText().toString()).equals(txtpassword.getText().toString())){ 

Calendar c1 = Calendar.getInstance(); 
SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy hh:mm"); 
String strdate1 = sdf1.format(c1.getTime()); 

TextView txtdate1 = (TextView) findViewById(R.id.textView1); 
txtdate1.setText(strdate1); 

gps = new GPSTracker(MainActivity.this); 

// check if GPS enabled 
if(gps.canGetLocation()){ 

    double latitude = gps.getLatitude(); 
    double longitude = gps.getLongitude(); 

    // \n is for new line 
    Toast.makeText(getApplicationContext(), "You have clock in succesfully at \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
}else{ 
    // can't get location 
    // GPS or Network is not enabled 
    // ask user to enable GPS/Network in settings 
    gps.showSettingsAlert(); 

} 
    } else{ 
    Toast.makeText(MainActivity.this, "Invalid Username//Password",Toast.LENGTH_LONG).show(); 
    } 

} 
}) ;  



btnclockout.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
    // TODO Auto-generated method stub 

    if((txtusername.getText().toString()).equals(txtpassword.getText().toString())){ 

     Calendar c1 = Calendar.getInstance(); 
     SimpleDateFormat sdf1 = new SimpleDateFormat("d/M/yy h:m a"); 
     String strdate1 = sdf1.format(c1.getTime()); 

     TextView txtdate1 = (TextView) findViewById(R.id.textView1); 
     txtdate1.setText(strdate1); 

     gps = new GPSTracker(MainActivity.this); 

     // check if GPS enabled 
     if(gps.canGetLocation()){ 


      double latitude = gps.getLatitude(); 
      double longitude = gps.getLongitude(); 


      // \n is for new line 
      Toast.makeText(getApplicationContext(), "You have clocked out successfully at \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
     }else{ 
      // can't get location 
      // GPS or Network is not enabled 
      // ask user to enable GPS/Network in settings 
      gps.showSettingsAlert(); 

     } 

      } else{ 
      Toast.makeText(MainActivity.this, "Invalid Username//Password",Toast.LENGTH_LONG).show(); 
      } 





}; 
}); 
} 


} 

回答

0

试试这个yourButton.setEnabled(true);true如果条件得到满足,否则false。如果需要,你可能想添加这个yourButton.setClickable(true);

+0

你碰巧知道条件代码吗?因为我没有编码背景。因此,我真的很虚弱。 – user3567749

+0

你说的问题不是很清楚,也许试图添加一些图解来解释整个问题。 – CENT1PEDE

+0

我试图解释原因,我不能把它放在这里作为它的机密。我现在需要的是参加考试申请。因此UI具有用户名,密码和2个按钮。这是时钟进出。在时钟输入按钮中,该按钮用于检查配置的设置是否允许“非工作现场”。意思是说用户可以在任何地方记时,并且Geopoint被记录到数据库中。如果设置允许,则继续对用户进行身份验证并将其地址发送到服务器。如果不允许,检查工地和当前的距离,看是否允许。 – user3567749

0
if (condition) { 
    btn.setEnabled(true); 
    btn.setOnClickListner(new ViewOnClickListener() { 
     @Override 
     public void onClick(final View v) { 
      // your code here 
     } 
    }); 
} else { 
    btn.setEnabled(false); 
}