我有两个按钮"员工"和"雇主"在名为" Selection"的活动中,如果"员工"按下按钮然后按两个按钮" addEmployee"和" generateReport"来自" Home"活动应该被禁用。
选择活动
public class Selection extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selection);
final Button btn_Employee = (Button) findViewById(R.id.btn_Employee);
Button btn_Employer = (Button) findViewById(R.id.btn_Employer);
btn_Employee.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vEmployee) {
if(btn_Employee.isPressed())
{
//markAttendance and generateReport button should be disabled
Intent i = new Intent(Selection.this, HomeActivity.class);
startActivity(i);
}
}
});
btn_Employer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vEmployer) {
}
});
}
}
家庭活动
public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ImageButton markAttendance = (ImageButton) findViewById(R.id.markAttendance);
ImageButton viewHolidays = (ImageButton) findViewById(R.id.viewHolidays);
ImageButton addEmployee = (ImageButton) findViewById(R.id.addEmployee);
ImageButton generateReport = (ImageButton) findViewById(R.id.generateReport);
Toolbar toolbar = (Toolbar) findViewById(R.id.sign_in_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(getTitle());
markAttendance.setOnClickListener(this);
viewHolidays.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holidays();
}
});
addEmployee.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AddEmployee();
}
});
generateReport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
report();
}
});
}
private void AddEmployee() {
Intent addIntent = new Intent(this, EmployeeDetails.class);
startActivity(addIntent);
}
private void report() {
Intent webIntent = new Intent(this, TempWeb.class);
startActivity(webIntent);
}
private void holidays() {
Intent holidayIntent = new Intent(this, Holiday.class);
startActivity(holidayIntent);
}
@Override
public void onClick(View v) {
attendance();
}
private void attendance() {
Intent attendanceIntent = new Intent(this, MarkAttendance.class);
startActivity(attendanceIntent);
}
}
感谢任何帮助!
答案 0 :(得分:2)
您可以通过在Home Acitivity
中识别可以在Selection Activity
中按下哪个按钮的意图传递一些数据来实现。
在按下员工按钮的选择活动中
Intent i = new Intent(Selection.this, HomeActivity.class);
intent.putExtra("IS_EMPLOYEE", true);
startActivity(i);
在家庭活动中检查是否按下员工按钮,并做相应的事情:
if(getIntent().getExtras().getBoolean("IS_EMPLOYEE")){
// do what you want to do on employee button press.
}
else{
// do what you want to do on employer button press.
}
当false
按下雇主按钮时,也会通过意图传递Selection Activity
值。
答案 1 :(得分:1)
在setEnabled(false)
onClickListener
到要禁用的按钮
如果您希望更方便地了解按钮是否被选中/按下,您可以使用按钮的setSelected(true)
(或错误)功能,这样您就可以随时询问它是否已选中< / p>
希望有所帮助:)
答案 2 :(得分:0)
只需在你的Intent to Home活动中按下哪个按钮,然后在你的家庭活动中获取意图数据,并使用if条件应用你的残疾按钮。
因此,如果您的Home Activity仅由Selection活动打开,那么您可以使用Intents传递此数据,否则您可以使用SharedPreference保存数据,然后在Home活动中检索此首选项。
要通过意图传递数据和检索数据,请参阅此处 - Passing Through intent
要通过共享首选项进行存储和检索,请参阅此处 - Shared Preference
答案 3 :(得分:0)
通过Intent传递值以检查调用哪个按钮
String buttonValue = getIntent().getStringExtra("BUTTON_CLICK");
并在家庭活动中获得意图值
$('#FromDate').on('blur', function() {
var fromdate = new Date("d-m-Y", $('#FromDate').val());
var gstdate = "1-7-2017";
var gd = new Date('d-m-Y', gstdate);
if (fromdate <= gst) {
//do something..
} else {
//do something..
}
});
答案 4 :(得分:0)
选择活动
btn_Employee.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vEmployee) {
if(btn_Employee.isPressed())
{
//markAttendance and generateReport button should be disabled
Intent i = new Intent(Selection.this, HomeActivity.class);
startActivity(i).putExtra("addEmployee", false).putExtra("reports", false));
}
}
});
家庭活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ImageButton markAttendance = (ImageButton) findViewById(R.id.markAttendance);
ImageButton viewHolidays = (ImageButton) findViewById(R.id.viewHolidays);
ImageButton addEmployee = (ImageButton) findViewById(R.id.addEmployee);
ImageButton generateReport = (ImageButton) findViewById(R.id.generateReport);
Toolbar toolbar = (Toolbar) findViewById(R.id.sign_in_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(getTitle());
//getExtra value
boolean addEmployee = getIntent().getBooleanExtra("addEmployee", false);
boolean reports = getIntent().getBooleanExtra("reports", false);
if(addEmployee){
addEmployee.setEnable(false);
}
if(reports){
generateReport.setEnable(false);
}
}
答案 5 :(得分:0)
没有意图的两个类之间的交互:
创建这样的Helper类:
public class ChangeButtonStatusHelper {
public interface ChangeButtonsListener{
void twoButtonsDisabled();
}
public static ChangeButtonStatusHelper mInstance;
private ChangeButtonsListener mListener;
private boolean mState;
private ChangeButtonStatusHelper(){
//do nothing
}
public static ChangeButtonStatusHelper getInstance(){
if(mInstance == null){
mInstance = new ChangeButtonStatusHelper();
}
return mInstance;
}
public void setListener(ChangeButtonsListener mListener){
this.mListener = mListener;
}
public void stateChange(boolean mState){
if(mListener!=null){
this.mState = mState;
notifyStateChange();
}
}
public boolean getState(){
return mState;
}
private void notifyStateChange() {
mListener.onPriorityChanged();
}
}
在onCreate () of HomeActivity.class
ChangeButtonStatusHelper.getInstance().setListener(new ChangeButtonsListener.twoButtonsDisabled() {
@Override
public void onPriorityChanged() {
if (ChangeButtonStatusHelper.getInstance().getState()) {
**Disable your buttons here**
}
}
});
SelectionActivity.class
中的
btn_Employee.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vEmployee) {
if(btn_Employee.isPressed())
{
//markAttendance and generateReport button should be disabled
ChangeButtonStatusHelper.getInstance().stateChange(true);
}
}
});
答案 6 :(得分:0)
//In Selection Activity
btn_Employee.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vEmployee) {
if(btn_Employee.isPressed())
{
//markAttendance and generateReport button should be disabled
Intent i = new Intent(Selection.this, HomeActivity.class);
i.putExtra("IsEmployeeBtnPressd",true);
startActivity(i);
}
}
// In Home Activity.
class HomeActivity extends extends AppCompatActivity {
@Override
oncreate(Bundle bundle){
ImageButton markAttendance = (ImageButton) findViewById(R.id.markAttendance);
ImageButton generateReport = (ImageButton) findViewById(R.id.generateReport)
boolean isEmployeeBtnPressed=getIntent().getBooleanExtra("IsEmployeeBtnPressd",false);
if(isEmployeeBtnPressed){
//You should disable
markAttendance.setEnabled(false);
generateReport.setEnabled(false);
}
}
}
答案 7 :(得分:0)
如果您希望这样做而不必传递Intent
,则可以通过注册OnSharedPreferenceChangeListener
来实现此行为。这是一个例子:
<强> HomeActivity.java:强>
public class HomeActivity extends AppCompatActivity implements View.OnClickListener,
SharedPreferences.OnSharedPreferenceChangeListener{
public static final String BTN_EMPLOYEE_PREFS_TAG = "employeeTag";
private ImageButton addEmployee;
private ImageButton generateReport;
private SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
addEmployee = (ImageButton) findViewById(R.id.addEmployee);
generateReport = (ImageButton) findViewById(R.id.generateReport);
.
.
.
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
.
.
.
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(BTN_EMPLOYEE_PREFS_TAG.equals(key)){
// listens to any changes made on this tag
boolean enableOrDisable = prefs.getBoolean(BTN_EMPLOYEE_PREFS_TAG, false);
addEmployee.setEnabled(enableOrDisable);
generateReport.setEnabled(enableOrDisable);
}
}
.
.
.
}
<强> Selection.java:强>
public class Selection extends AppCompatActivity{
private SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selection);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
findViewById(R.id.btn_Employee).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vEmployee) {
if(btn_Employee.isPressed()){
prefs.edit().putBoolean(BTN_EMPLOYEE_PREFS_TAG, false).apply();
// rest of your logic goes here.
}
}
});
.
.
.
}
.
.
.
}