gpt4 book ai didi

java - 如何判断Button是否被点击

转载 作者:行者123 更新时间:2023-12-02 04:50:29 24 4
gpt4 key购买 nike

我必须检查按钮是否被单击。如果单击的应用程序必须执行一项任务,如果没有,应用程序必须执行另一项任务。我尝试这样做,但没有收到连接错误,我已将其放在 catch block 中的代码末尾。

 protected void onCreate(Bundle savedInstanceState) {
PracticeVO practiceObj;

try {

setTitle("Klarity");
setPrefBtn = (Button) findViewById(R.id.setPrefBtn);

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_klarity_home);

/*
* asynchronous calls
*/

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);

final ConnectionHelper con = new ConnectionHelper();
/*
* It will connect to DB and fetches the Practice Information
*/
if (Btnclicked == false) {

String allPracticesStr = null;

这里我将 boolean 变量“Btnclicked”设置为 true。

 setPrefBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Btnclicked=true;
Intent setPrefIntent = new Intent(KlarityHome.this,
SetPreferences.class);
startActivity(setPrefIntent);

}
});

但是执行完之后,光标直接到这里,显示“无连接”。

catch (Exception ex) {
Context context = getApplicationContext();
CharSequence text = "There is some error in application";
int duration = Toast.LENGTH_LONG;

Toast toast = Toast.makeText(context, text, duration);
toast.show();
System.out.println("no connection");
}

}

任何人都有解决方案。

最佳答案

正如你所说

"the button is clicked or not.if clicked application has to do one task and if noapplication has to do another task" so, if i were you, i'll put 2 radio buttons, every one with the text of the task you want to do, and add a listener for them, like this:

radiobutton1.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
radiobutton2.setChecked(false);
}
});

radiobutton2.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
radiobutton1.setChecked(false);
}
});

最后,在按钮中执行您想要执行的任务:

    Button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
if(radiobutton1.isChecked){
//do task 1
}else{
//do task2}
}
});

关于java - 如何判断Button是否被点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29285204/

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