2012-07-25 53 views
0

我有三個活動,其中第一個活動是歡迎應用程序頁面(SavingsGuiderSplashActivity),它將爲要求用戶名(SavingsGuiderUserActivity)的第二個活動生成動畫。我使用共享首選項來存儲用戶名。然後點擊提交按鈕後,它將進入菜單頁面(SavingsGuiderMenuActivity)。 ive在第二個頁面聲明瞭檢索首選方法,所以當用戶再次啓動應用程序時,如果prefs包含用戶名,它將直接進入主菜單活動頁面而不是詢問用戶名的第二個活動。我認爲問題更多關於SavingsGuiderSplashActivity和SavingsGuiderUserActivity。儘管我在菜單頁上顯示名稱時沒有問題。 (「Hi John」)。我試圖產生這個,但不知何故,我第二次啓動應用程序,它仍然會進入第二頁。有人可以告訴我我的代碼有什麼問題嗎?Android:第二次啓動時跳過用戶詳細信息輸入頁面

我的動畫歡迎頁面代碼:

public class SavingsGuiderSplashActivity extends SavingsActivity { 


EditText nameEdit; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 
    startAnimating(); 
} 

private void startAnimating() { 
    // Fade in top title 
    TextView img1 = (TextView) findViewById(R.id.TextViewTopTitle); 
    Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fade_in); 
    img1.startAnimation(fade1); 
    // Fade in bottom title after a built-in delay. 
    TextView img2 = (TextView) findViewById(R.id.TextViewBottomTitle); 

    Animation fade2 = AnimationUtils.loadAnimation(this, R.anim.fade_in2); 
    img2.startAnimation(fade2); 

    // Transition to Main Menu when bottom title finishes animating 
    fade2.setAnimationListener(new AnimationListener() { 
     public void onAnimationEnd(Animation animation) { 

      // The animation has ended, transition to the Main Menu screen    

       startActivity(new Intent(SavingsGuiderSplashActivity.this, SavingsGuiderUserActivity.class)); 
      SavingsGuiderSplashActivity.this.finish(); 
     } 


    @Override 
    public void onAnimationRepeat(Animation animation) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onAnimationStart(Animation animation) { 
     // TODO Auto-generated method stub 

    }}); 

    // Load animations for all views within the TableLayout 

    Animation spinin = AnimationUtils.loadAnimation(this, R.anim.custom_anim); 
    LayoutAnimationController controller = new LayoutAnimationController(spinin); 
    TableLayout table = (TableLayout) findViewById(R.id.tableLayout1); 
    for (int i = 0; i < table.getChildCount(); i++) { 
     TableRow row = (TableRow) table.getChildAt(i); 
     row.setLayoutAnimation(controller); 

    } 
} 


     @Override 
     protected void onPause() { 
      super.onPause(); 
      // Stop the animation 
      TextView img1 = (TextView) findViewById(R.id.TextViewTopTitle); 
      img1.clearAnimation(); 
      TextView img2 = (TextView) findViewById(R.id.TextViewBottomTitle); 
      img2.clearAnimation(); 

      TableLayout table = (TableLayout) findViewById(R.id.tableLayout1); 
      for (int i = 0; i < table.getChildCount(); i++) { 
       TableRow row = (TableRow) table.getChildAt(i); 
       row.clearAnimation(); 



     } 
     } 

     @Override 
     protected void onResume() { 
      super.onResume(); 

      // Start animating at the beginning 
      startAnimating(); 

     } 

}

我的用戶活動頁面在這裏:

public class SavingsGuiderUserActivity extends SavingsActivity { 
/** Called when the activity is first created. */ 

    String tag = "SavingsGuiderActivity"; 
    EditText nameEdit; 
    Toast toast;   

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    retrievePreferences(); 
    setContentView(R.layout.user);   

    Button submitBtn = (Button)findViewById(R.id.btn_submit); 
    nameEdit=(EditText)findViewById(R.id.edit_name); 

    submitBtn.setOnClickListener(new View.OnClickListener() {   
     public void onClick(View v) { 
       String txt = nameEdit.getText().toString();     

       //validate the editText 
       if (!txt.equals("")) {      
       Intent intent = new Intent(SavingsGuiderUserActivity.this, SavingsGuiderMenuActivity.class); 
       Bundle extras = new Bundle(); 
       extras.putString("name",txt); 
       intent.putExtras(extras); 
       saveAsPreferences(); 
       startActivity(intent); 

       } 
       else { 
        Context context = getApplicationContext(); 
        CharSequence text = "Please enter your name!"; 
        int duration = Toast.LENGTH_SHORT; 
        Toast toast = Toast.makeText(context, text, duration); 
        toast.show(); 
       }     

     } 
    }); 

} 

@Override 
protected void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    Log.d(tag, "In the onDestroy() event"); 
} 
@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 

    Log.d(tag, "In the onPause() event"); 
} 
@Override 
protected void onRestart() { 
    // TODO Auto-generated method stub 
    super.onRestart(); 
    Log.d(tag, "In the onRestart() event"); 
    retrievePreferences(); 
} 
@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume();  
    Log.d(tag, "In the onResume() event"); 
    retrievePreferences(); 
} 
@Override 
protected void onStart() { 
    // TODO Auto-generated method stub 
    super.onStart(); 
    Log.d(tag, "In the onStart() event"); 
    retrievePreferences(); 
} 
@Override 
protected void onStop() { 
    // TODO Auto-generated method stub 
    super.onStop(); 
    Log.d(tag, "In the onStop() event"); 
} 
public void saveAsPreferences(){ 
String nameString = nameEdit.getText().toString(); 
SharedPreferences prefs = getSharedPreferences("preferences", MODE_PRIVATE); 
SharedPreferences.Editor editor = prefs.edit(); 
editor.putString("name", nameString); 
} 
public void retrievePreferences(){ 
SharedPreferences prefs = getSharedPreferences("preferences",MODE_PRIVATE); 
if(prefs.contains("name")){ 
String nameString = prefs.getString("name", ""); 
nameEdit.setText(nameString); 
Intent intent = new Intent(SavingsGuiderUserActivity.this, SavingsGuiderMenuActivity.class); 
Bundle extras = new Bundle(); 
intent.putExtras(extras); 
startActivity(intent); 
} 

} 

}

我的菜單頁面在這裏:

public class SavingsGuiderMenuActivity extends SavingsActivity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.menu); 

    Bundle bundle = getIntent().getExtras(); 
    String name= bundle.getString("name"); 

TextView resultView =(TextView)findViewById(R.id.view_Name);

resultView.setText("Welcome " + name); 

    ListView menuList = (ListView) findViewById(R.id.ListView_Menu); 
    String[] items = { getResources().getString(R.string.start), 
      getResources().getString(R.string.about), 
      getResources().getString(R.string.help) }; 
    ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu_item, items); 
    menuList.setAdapter(adapt); 
    menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) { 
      // Note: if the list was built "by hand" the id could be used. 
      // As-is, though, each item has the same id 
      TextView textView = (TextView) itemClicked; 
      String strText = textView.getText().toString(); 
      if (strText.equalsIgnoreCase(getResources().getString(R.string.start))) { 
       // Launch the Game Activity 
       startActivity(new Intent(SavingsGuiderMenuActivity.this, SavingsGuiderAppActivity.class)); 
      } else if (strText.equalsIgnoreCase(getResources().getString(R.string.help))) { 
       // Launch the Help Activity 
       startActivity(new Intent(SavingsGuiderMenuActivity.this, SavingsGuiderHelpActivity.class)); 
      } else if (strText.equalsIgnoreCase(getResources().getString(R.string.about))) { 
       // Launch the Settings Activity 
       startActivity(new Intent(SavingsGuiderMenuActivity.this, SavingsGuiderAboutActivity.class)); 
      } 
     } 
    }); 
} 

} 

回答

1

嘗試在第一頁本身(即在SavingsGuiderSplashActivity)檢索您的喜好並查看用戶名是否存在。使用下面的函數

public boolean usernameExists() 
{ 
SharedPreferences prefs = getSharedPreferences("preferences",MODE_PRIVATE); 
if(prefs.contains("name")) 
{ 
return true; 
} 
else 
{ 
return false; 
} 
} 

等待,我就改變你的SavingsGuiderSplashActivity代碼爲您

public class SavingsGuiderSplashActivity extends SavingsActivity { 


EditText nameEdit; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.splash); 
startAnimating(); 
} 


private void startAnimating() { 
// Fade in top title 
TextView img1 = (TextView) findViewById(R.id.TextViewTopTitle); 
Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fade_in); 
img1.startAnimation(fade1); 
// Fade in bottom title after a built-in delay. 
TextView img2 = (TextView) findViewById(R.id.TextViewBottomTitle); 

Animation fade2 = AnimationUtils.loadAnimation(this, R.anim.fade_in2); 
img2.startAnimation(fade2); 

// Transition to Main Menu when bottom title finishes animating 
fade2.setAnimationListener(new AnimationListener() { 
    public void onAnimationEnd(Animation animation) { 

     // The animation has ended, transition to the Main Menu screen    

     if(!usernameExists()) 
     { 
      startActivity(new Intent(SavingsGuiderSplashActivity.this, SavingsGuiderUserActivity.class)); 
     } 
      else 
      { 
      startActivity(new Intent(SavingsGuiderSplashActivity.this, SavingsGuiderMenuActivity.class)); 
      } 
     SavingsGuiderSplashActivity.this.finish(); 
    } 

public boolean usernameExists() 
{ 
SharedPreferences prefs = getSharedPreferences("preferences",MODE_PRIVATE); 
if(prefs.contains("name")) 
{ 
return true; 
} 
else 
{ 
return false; 
} 
} 

@Override 
public void onAnimationRepeat(Animation animation) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onAnimationStart(Animation animation) { 
    // TODO Auto-generated method stub 

}}); 

// Load animations for all views within the TableLayout 

Animation spinin = AnimationUtils.loadAnimation(this, R.anim.custom_anim); 
LayoutAnimationController controller = new LayoutAnimationController(spinin); 
TableLayout table = (TableLayout) findViewById(R.id.tableLayout1); 
for (int i = 0; i < table.getChildCount(); i++) { 
    TableRow row = (TableRow) table.getChildAt(i); 
    row.setLayoutAnimation(controller); 

} 
} 


    @Override 
    protected void onPause() { 
     super.onPause(); 
     // Stop the animation 
     TextView img1 = (TextView) findViewById(R.id.TextViewTopTitle); 
     img1.clearAnimation(); 
     TextView img2 = (TextView) findViewById(R.id.TextViewBottomTitle); 
     img2.clearAnimation(); 

     TableLayout table = (TableLayout) findViewById(R.id.tableLayout1); 
     for (int i = 0; i < table.getChildCount(); i++) { 
      TableRow row = (TableRow) table.getChildAt(i); 
      row.clearAnimation(); 



    } 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     // Start animating at the beginning 
     startAnimating(); 

    } 

這應該修復它!

+0

謝謝!我是否需要刪除其他活動頁面中的任何代碼,或只添加上面的代碼? – honeybee 2012-07-25 13:37:34

+0

嘿,我試過了,但是它仍然會在第二次啓動時提示輸入用戶名......實際上我第二次啓動時,它進入了用戶頁面n跳過了啓動動畫頁面。 – honeybee 2012-07-25 13:48:44

+0

這真的很奇怪。您需要刪除用戶頁面中的retrievePreferences。你只需要檢查用戶名是否存在於第一頁,如果它存在,你可以跳到第三頁。這就是這段代碼的用法(!usernameExists()) { startActivity(new Intent(SavingsGuiderSplashActivity.this,SavingsGuiderUserActivity.class)); } else startActivity(new Intent(SavingsGuiderSplashActivity.this,SavingsGuiderMenuActivity.class)); } – 2012-07-26 07:00:50

0

在,如果你有一個用戶名檢索偏好的底部,可以嘗試:

this.finish(); 
+0

你的意思是在用戶活動頁面,在檢索首選項方法之後startActivity(意向);,我把this.finish( ); ? – honeybee 2012-07-25 13:29:57

+0

好的ive添加了它。第一次啓動應用程序時,它會提示輸入我的名字。在我輸入之後,它會引導我進入菜單頁面。然後當我按下硬件後退按鈕一路到Android主頁並重新啓動我的應用程序,它仍然提示我爲我的名字.. – honeybee 2012-07-25 13:32:54

0

如果我理解正確的話,你要直接從SavingsGuiderSplashActivity移動第二次 - >SavingsGuiderMenuActivity,有跳過SavingsGuiderUserActivity。如果這是真的,那麼在你的SavingsGuiderSplashActivity的 onAnimationEnd方法,你需要檢查,如果優先值設置,並根據您需要推出相應的活動,

+0

是的,你是正確的。但我如何檢查他們?我是否需要重新聲明共享並在splash活動中重新檢索,然後執行if else語句? – honeybee 2012-07-25 13:28:10

0

所以這就是我的老師教我讓愛應用程序運行!

用戶活動頁面

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.user);   

    Button submitBtn = (Button)findViewById(R.id.btn_submit); 
    nameEdit=(EditText)findViewById(R.id.edit_name); 

    submitBtn.setOnClickListener(new View.OnClickListener() {   
     public void onClick(View v) { 
       String txt = nameEdit.getText().toString();     

       //validate the editText 
       if (!txt.equals("")) {      
       Intent intent = new Intent(SavingsGuiderUserActivity.this, SavingsGuiderMenuActivity.class); 
       Bundle extras = new Bundle(); 
       extras.putString("name",txt); 
       intent.putExtras(extras); 
       saveAsPreferences(); 
       startActivity(intent); 

       } 
       else { 
        Context context = getApplicationContext(); 
        CharSequence text = "Please enter your name!"; 
        int duration = Toast.LENGTH_SHORT; 
        Toast toast = Toast.makeText(context, text, duration); 
        toast.show(); 
       }     

     } 
    }); 

} 

@Override 
protected void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    Log.d(tag, "In the onDestroy() event"); 
} 
@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 

    Log.d(tag, "In the onPause() event"); 
} 
@Override 
protected void onRestart() { 
    // TODO Auto-generated method stub 
    super.onRestart(); 
    Log.d(tag, "In the onRestart() event"); 
} 
@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume();  
    Log.d(tag, "In the onResume() event"); 
} 
@Override 
protected void onStart() { 
    // TODO Auto-generated method stub 
    super.onStart(); 
    Log.d(tag, "In the onStart() event"); 
} 
@Override 
protected void onStop() { 
    // TODO Auto-generated method stub 
    super.onStop(); 
    Log.d(tag, "In the onStop() event"); 
} 
public void saveAsPreferences(){ 
String nameString = nameEdit.getText().toString(); 
SharedPreferences prefs = getSharedPreferences("preferences", MODE_PRIVATE); 
SharedPreferences.Editor editor = prefs.edit(); 
editor.clear(); 
editor.putString("name", nameString); 
editor.commit(); 
} 
} 

飛濺活動:

public void onAnimationEnd(Animation animation) { 

      // The animation has ended, transition to the Main Menu screen 
      if(!usernameExists()) 
      { 
       startActivity(new Intent(SavingsGuiderSplashActivity.this, SavingsGuiderUserActivity.class)); 
      } 
      else 
      { 
       Intent intent = new Intent(SavingsGuiderSplashActivity.this, SavingsGuiderMenuActivity.class); 
       Bundle extras = new Bundle(); 
       extras.putString("name",strName); 
       intent.putExtras(extras); 
       startActivity(intent);     
      } 
      SavingsGuiderSplashActivity.this.finish(); 
     } 

    public boolean usernameExists() 
    { 
    SharedPreferences prefs = getSharedPreferences("preferences",MODE_PRIVATE); 
    if(prefs.contains("name")) 
    { 
     strName = prefs.getString("name", null); 
    return true; 
    } 
    else 
    { 
    return false; 
    } 
    } 
+0

接受我的答案然後稍微修改並且不接受我的答案並將其作爲自己的答案發布的意義何在?你可能只是要求我用這4行來編輯我的代碼。 Bundle extras = new Bundle(); extras.putString(「name」,strName); intent.putExtras(extras); startActivity(intent); – 2012-07-28 09:47:59

+0

對於人們在回答您的問題時所付出的努力表示尊重。 – 2012-07-28 09:49:33

+0

我接受了!蜱是綠色的!對不起,如果我冒犯了你,因爲我剛剛進入這個網站!最初,我勾選了人們給我的所有答案,然後我發現我只能勾選出「未接通知」。然後我馬上勾選你的答案,因爲沒有你給我的代碼,我會卡住到現在。我發誓一直都有綠剔。 – honeybee 2012-07-28 11:14:27