我对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 :(得分:0)
yourButton.setEnabled(true);
true
,否则false
。如果需要,您可以添加此yourButton.setClickable(true);
。
答案 1 :(得分:0)
if (condition) {
btn.setEnabled(true);
btn.setOnClickListner(new ViewOnClickListener() {
@Override
public void onClick(final View v) {
// your code here
}
});
} else {
btn.setEnabled(false);
}