我想仅在某个操作完成时禁用后退按钮。但是如果我覆盖onBackPressed()并在我所需的操作中调用它,则从开始就禁用后退按钮。我希望禁用后退按钮只有当某个动作不是从一开始就完成时。请帮助。
package com.example.shopkart;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;
public class creditcard extends Activity {
datamanager dm;
String[] productnamearray;
boolean[] checkarray;
String name,mailid,number,result;
int[] id_array;
EditText ccnumber;
Button btnccconfirm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.creditcard);
dm=new datamanager(this);
ccnumber=(EditText) findViewById(R.id.ccnumber);
btnccconfirm=(Button) findViewById(R.id.btnccconfirm);
productnamearray=getIntent().getExtras().getStringArray("productnamearray");
checkarray=getIntent().getExtras().getBooleanArray("checkarray");
id_array=getIntent().getExtras().getIntArray("id_array");
name=getIntent().getExtras().getString("name");
mailid=getIntent().getExtras().getString("mailid");
btnccconfirm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
number=ccnumber.getText().toString();
if(number.length()!=16)
Toast.makeText(getApplicationContext(),"please enter a number of 16 digits",Toast.LENGTH_LONG).show();
else if(number.length()==16)
{
result=validate(number);
}
if(result=="Card is Valid")
{
dm.finalupdate(mailid,productnamearray,checkarray,id_array);
Toast.makeText(getApplicationContext(),"Transaction completed!",Toast.LENGTH_LONG).show();
btnccconfirm.setVisibility(View.INVISIBLE);
onBackPressed();
}
else if(result=="Card is Invalid")
{
Toast.makeText(getApplicationContext(),"Invalid credit card number",Toast.LENGTH_LONG).show();
}
}
});
}
public String validate(String ccnum)
{
if(ccnum.length()==16){
char[] c = ccnum.toCharArray();
int[] cint = new int[16];
for(int i=0;i<16;i++){
if(i%2==1){
cint[i] = Integer.parseInt(String.valueOf(c[i]))*2;
if(cint[i] >9)
cint[i]=1+cint[i]%10;
}
else
cint[i] = Integer.parseInt(String.valueOf(c[i]));
}
int sum=0;
for(int i=0;i<16;i++){
sum+=cint[i];
}
if(sum%10==0)
return "Card is Valid";
else
return "Card is Invalid";
}else
return "Card is Invalid";
}
@Override
public void onBackPressed() {
}
}
我只在结果值为Card有效时调用Onbackpressed()方法。我不希望从开头就禁用后退按钮。
答案 0 :(得分:2)
您的问题非常模糊,不确定您正在谈论哪种行动。更多信息会有所帮助。
无论如何,这是一个通用的代码片段,只需根据需要设置布尔值
// An attribute to trigger default back pressed behaviour
protected boolean actionDone = false;
// use this to set actionDone to true, when whatever action it may be is triggered
public void onSomeActionDone()
{
actionDone = true;
}
@ Override
public void onBackPressed()
{
if (actionDone){ // use default onBackPressed behaviour, going up the navigation or exiting the app
super.onBackPressed ();
} else {
// do nothing, maybe fire a toast saying: "cant go back"
}
}
在OP添加他们的代码片段后编辑:
代码中有一些错误会导致一些错误:
小注:检查Java约定以获取类命名(我使用Google Java Style:https://google-styleguide.googlecode.com/svn/trunk/javaguide.html)。课程应以大写字母开头。
我会让validate(String ccnum)
返回boolean
而不是String
,这是更好的做法。如果你想打印&#34;它有效/无效&#34;某处,只需检查布尔值即可。此外,您可以将validate(String ccnum)
设为静态,因为它是一种验证方法,并且不需要操纵对象状态,例如:
public static boolean validate(String ccnum)
{
// ... -snip-
if(sum%10==0)
return true;
else
return false;
}else
return false;
}
另外,请注意onClick
方法中的字符串比较。使用==
不是字符串比较,而是对象比较。您应该使用String.equals(String other)
或String.equalsIgnoreCase(String other)
,并注意您评估result
变量的位置。使用&#34; **&#34;查找评论。在它面前:
public void onClick(View v) {
number=ccnumber.getText().toString();
if(number.length()!=16)
Toast.makeText(getApplicationContext(),"please enter a number of 16 digits",Toast.LENGTH_LONG).show();
else if(number.length()==16)
{
result=validate(number);
// ** moved if ... else here. The value of result will atleast be set
if(result.equals("Card is Valid")) // more correct comparison, but as recommended above, use a boolean instead, ie : if (result)
{
dm.finalupdate(mailid,productnamearray,checkarray,id_array);
Toast.makeText(getApplicationContext(),"Transaction completed!",Toast.LENGTH_LONG).show();
btnccconfirm.setVisibility(View.INVISIBLE);
// ** Why are you calling onBackPressed here? onBackPressed is an activity
// method called when the android 'back' button is pressed by the user. If
// you wanna exit out of the activity, use Activity.finish() or use
// activity.setResult(), then call Activity.finish() (look up the developer docs)
onBackPressed();
}
else if(result.equals("Card is Invalid"))
{
Toast.makeText(getApplicationContext(),"Invalid credit card number",Toast.LENGTH_LONG).show();
}
}
}
如果您让validate(String ccnum)
返回boolean
,则可以这样做:
// -SNIP-
if(validate(number))
{
dm.finalupdate(mailid,productnamearray,checkarray,id_array);
Toast.makeText(getApplicationContext(),"Transaction completed!",Toast.LENGTH_LONG).show();
btnccconfirm.setVisibility(View.INVISIBLE);
onBackPressed();
}
else
{
Toast.makeText(getApplicationContext(),"Invalid credit card number",Toast.LENGTH_LONG).show();
}