我使用身份验证进入页面,经过身份验证后只有用户进入页面。我为onbackpressed()编写了一个代码,但它无效。这里DatabaseDemo和Login是两个类。当我按下后退按钮时,显示用户名和密码的登录类。
DatabaseDemo.java
public class DatabaseDemo extends TabActivity {
DatabaseHelper dbHelper;
GridView grid;
TextView txtTest;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SetupTabs();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(1, 1, 1, "Add Employee");
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
//Add employee
case 1:
Intent addIntent=new Intent(this,AddEmployee.class);
startActivity(addIntent);
break;
}
super.onOptionsItemSelected(item);
return false;
}
void SetupTabs()
{
TabHost host=getTabHost();
TabHost.TabSpec spec=host.newTabSpec("tag1");
Intent in1=new Intent(this, AddEmployee.class);
spec.setIndicator("Add Employee");
spec.setContent(in1);
TabHost.TabSpec spec2=host.newTabSpec("tag2");
Intent in2=new Intent(this, GridList.class);
spec2.setIndicator("Employees");
spec2.setContent(in2);
host.addTab(spec);
host.addTab(spec2);
}
@Override
public void onBackPressed()
{
Intent i = new Intent(DatabaseDemo.this, Login.class);
startActivity(i);
}
}
我还有一些课程,对其他意图工作正常。
Login.java
public class Login extends Activity implements OnClickListener{
Button btn;
EditText et1, et2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
btn = (Button)findViewById(R.id.btnlogin);
btn.setOnClickListener(this);
et1 = (EditText)findViewById(R.id.Leditename);
et2 = (EditText)findViewById(R.id.Leditpw);
}
@Override
public void onBackPressed()
{
super.onBackPressed();
finish();
}
@Override
public void onClick(View v) {
String ename = et1.getText().toString().trim();
System.out.println("ename is..." +ename);
String epw = et2.getText().toString().trim();
System.out.println("Password is..." +epw);
if(ename.equals("srikanth") && epw.equals("12345")){
Toast.makeText(getApplicationContext(), "valid login..!", Toast.LENGTH_LONG).show();
Intent in = new Intent(getApplicationContext(), DatabaseDemo.class);
startActivity(in);
}
else
{
Toast.makeText(getApplicationContext(), "Invalid authentication..!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), Login.class);
startActivity(intent);
}
}
Logcat没有显示错误。
02-14 17:45:33.595: I/ActivityManager(59): Starting activity: Intent { cmp=com.android.databaseex/.DatabaseDemo }
02-14 17:45:34.835: I/ActivityManager(59): Displayed activity com.android.databaseex/.DatabaseDemo: 1141 ms (total 1141 ms)
02-14 17:45:50.145: I/System.out(613): ename is...srikanth
02-14 17:45:50.145: I/System.out(613): Password is...12345
02-14 17:45:50.175: I/ActivityManager(59): Starting activity: Intent { cmp=com.android.databaseex/.DatabaseDemo }
02-14 17:45:51.055: I/ActivityManager(59): Displayed activity com.android.databaseex/.DatabaseDemo: 819 ms (total 819 ms)
答案 0 :(得分:1)
您需要跳过super.onBackPressed();
行:
@Override
public void onBackPressed()
{
Intent i = new Intent(DatabaseDemo.this, Login.class);
startActivity(i);
}
答案 1 :(得分:1)
而不是onBackpressed方法使用onkeydown()
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK){
Intent i = new Intent(DatabaseDemo.this, Login.class);
startActivity(i);
}
return super.onKeyDown(keyCode, event);
}
答案 2 :(得分:0)
@Override
public void onBackPressed()
{
Intent i = new Intent(DatabaseDemo.this, Login.class);
startActivity(i);
}
这是完美的工作,请先检查你的androidmanifest文件,是否有活动登录入口?如果有,那么没有任何问题,否则使活动登录清单文件
答案 3 :(得分:0)
在活动中覆盖onBackPressed方法
@Override
public void onBackPressed(){
Intent i = new Intent(DatabaseDemo.this, Login.class);
startActivity(i);
}