gpt4 book ai didi

安卓工作室 : CardView change function once clicked

转载 作者:行者123 更新时间:2023-11-29 02:32:05 26 4
gpt4 key购买 nike

我目前正在使用 CardView 作为 Activity 中的开始按钮。但是,一旦我单击了 CardView,我就需要背景颜色随文本和功能一起更改为我的 STOP Activity 。

  public class LandingPage extends AppCompatActivity implements View.OnClickListener {

private CardView appsCard, parentalControlsCard, customSettingsCard, activateCard, StartStopCard;
private TextView lockStatus, processStatus;
private AlarmManager alarmManager;
private ArrayList<RuleSet> ruleSets = null;
private boolean mStarted = false;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
lockStatus = (TextView) findViewById(R.id.onOff);
processStatus = (TextView) findViewById(R.id.processStartStop);
StartStopCard = (CardView) findViewById(R.id.StartStopCard);

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

final Intent intent = new Intent(LandingPage.this, LockOptionReceiver.class);
SharedPreferences setting = getSharedPreferences("PREFS", 0);
String switcher = setting.getString("lockStatus", "");
setStatus(switcher);
String switcher2 = setting.getString("processStatus" , "");
setProcess(switcher2);


try {
ruleSets = RuleSetList.retrieveRuleSet(this);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}


//Defining Cards on Landing Page
appsCard = (CardView) findViewById(R.id.apps_card);
parentalControlsCard = (CardView) findViewById(R.id.parentalControls_id);
customSettingsCard = (CardView) findViewById(R.id.customSettings);
activateCard = (CardView) findViewById(R.id.activate_id);
StartStopCard = (CardView) findViewById(R.id.StartStopCard);


//Add OnClick Listeners
appsCard.setOnClickListener(this);
parentalControlsCard.setOnClickListener(this);
customSettingsCard.setOnClickListener(this);
StartStopCard.setOnClickListener(this);
activateCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ruleSets.isEmpty()) {
Toast.makeText(LandingPage.this, "You did not create a custom setting.", Toast.LENGTH_SHORT).show();
} else {
PendingIntent pending_start;
PendingIntent pending_stop;
Intent startIntent = new Intent(LandingPage.this, LockOptionReceiver.class);
Intent stopIntent = new Intent(LandingPage.this, LockOptionReceiver.class);
Calendar startTime = new GregorianCalendar();
Calendar endTime = new GregorianCalendar();
String startString = ruleSets.get(0).getStartTime();
String endString = ruleSets.get(0).getEndTime();

String[] startArr = startString.split(":");
String[] endArr = endString.split(":");

startTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(startArr[0]));
startTime.set(Calendar.MINUTE, Integer.parseInt(startArr[1]));

endTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(endArr[0]));
endTime.set(Calendar.MINUTE, Integer.parseInt(endArr[1]));

pending_start = PendingIntent.getBroadcast(LandingPage.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
pending_stop = PendingIntent.getBroadcast(LandingPage.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

startIntent.putExtra("status", "start");
stopIntent.putExtra("status", "stop");

Toast.makeText(LandingPage.this, "Your ruleset will start at " + startString, Toast.LENGTH_SHORT).show();

setStatus("Lock Active");
setProcess("Stop");

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmManager.INTERVAL_DAY, startTime.getTimeInMillis(), pending_start);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmManager.INTERVAL_DAY, startTime.getTimeInMillis(), pending_stop);
}
}
});


final CardView StartStopCard = (CardView) findViewById(R.id.StartStopCard);
StartStopCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mStarted) {
mStarted=false;
SharedPreferences setting = getSharedPreferences("PREFS", 0);
SharedPreferences.Editor editor = setting.edit();
editor.remove("switcher");
editor.remove("switcher2");
editor.remove("lockStatus");
editor.remove("processStatus");
editor.commit();
editor.putString("switcher", "true");
editor.putString("switcher2", "true");
editor.putString("lockStatus", "Lock Active");
editor.putString("processStatus", "Start");
editor.apply();
intent.putExtra("status", "start");
sendBroadcast(intent);
intent.putExtra("processStatus", "start");
sendBroadcast(intent);
String status = setting.getString("lockStatus", "");
setStatus(status);
String processStatus = setting.getString("processStatus" , "");
setProcess(processStatus);

}

else {
mStarted= true;
SharedPreferences setting = getSharedPreferences("PREFS", 0);
SharedPreferences.Editor editor = setting.edit();
editor.remove("switcher");
editor.remove("switcher2");
editor.remove("lockStatus");
editor.remove("processStatus");
editor.commit();
editor.putString("switcher", "false");
editor.putString("switcher2", "false");
editor.putString("lockStatus", "Lock Deactivated");
editor.putString("processStatus", "Stop");
editor.apply();
intent.putExtra("status", "stop");
sendBroadcast(intent);
intent.putExtra("processStatus", "start");
sendBroadcast(intent);
String status = setting.getString("lockStatus", "");
setStatus(status);
String processStatus = setting.getString("processStatus" , "");
setProcess(processStatus);

}
}

});

}



@Override
public void onClick(View v) {

Intent i;

switch (v.getId()) {
case R.id.apps_card:
i = new Intent(this, AppList.class);
startActivity(i);
break;
case R.id.parentalControls_id:
i = new Intent(this, ParentalWelcomeActivity.class);
startActivity(i);
break;
case R.id.customSettings:
i = new Intent(this, ViewRuleSets.class);
startActivity(i);
break;
case R.id.activate_id:
i = new Intent(this, RuleSet.class);
startActivity(i);
break;
default:
break;

}
}



private void setStatus(String s) {
lockStatus.setText(s);
}

private void setProcess(String s) { processStatus.setText(s);}


public void ruleSet(View view) {
Intent intent = new Intent(this, ViewRuleSets.class);
startActivity(intent);
}


}

我目前正在使用 CardView 作为 Activity 中的开始按钮。但是,一旦我点击了 CardView,我需要背景颜色随文本和功能一起更改为我的 STOP Activity 。

最佳答案

例如,设置一个名为 mStarted 的类成员变量,并将其初始设置为“false”。

private boolean mStarted = false;

现在在您的 onClick() 监听器中,翻转此变量的值,然后检查它以确定操作过程。

CardView startStopCard = (CardView) findViewById(R.id.StartStopCard);
startStopCard .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mStarted = !mStarted;
if (mStarted) {
// do start stuff
} else {
// do stop stuff
}
}
}

关于安卓工作室 : CardView change function once clicked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49114104/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com