2012-07-25 3 views
0

첫 번째 활동은 사용자 이름 (SavingsGuiderUserActivity)을 묻는 두 번째 활동에 애니메이션을 적용하는 환영 앱 페이지 (SavingsGuiderSplashActivity)입니다. 공유 환경 설정을 사용하여 사용자 이름을 저장했습니다. 제출 버튼을 클릭하면 메뉴 페이지 (SavingsGuiderMenuActivity)로 이동합니다. 필자가 두 번째 페이지에서 환경 설정 메소드를 검색하도록 선언 했으므로 사용자가 응용 프로그램을 다시 시작할 때 사용자 이름이 포함되어 있으면 사용자 이름을 묻는 두 번째 활동 대신 주 메뉴 활동 페이지로 직접 이동합니다. 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; 
} 
} 

대기를 사용, 난 그냥 당신

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(); 

    } 

이 그것을 수정해야 대한 SavingsGuiderSplashActivity 코드를 변경거야!

+0

감사합니다! 다른 활동 페이지에서 코드를 삭제해야합니까, 아니면 위의 코드를 추가해야합니까? – honeybee

+0

이봐, 필자는 시도했지만 실제로 두 번째로 시작할 때 사용자 이름을 묻는다 ... 실제로 두 번째로 시작한 것은 사용자 페이지 n에 스플래시 애니메이션 페이지를 스킵했다. – honeybee

+0

정말 이상하다. 사용자 페이지에서 retrievePreferences를 삭제해야합니다. 첫 번째 페이지에 사용자 이름이 있는지 확인하기 만하면 세 번째 페이지로 건너 뛸 수 있습니다. 이것이이 코드의 기능입니다 (! usernameExists()) { startActivity (새 인 텐트 (SavingsGuiderSplashActivity.this, SavingsGuiderUserActivity.class)); } else { startActivity (새 인 텐트 (SavingsGuiderSplashActivity.this, SavingsGuiderMenuActivity.class))); } –

0

, 퍼팅 시도 : 내가 제대로 이해한다면

this.finish(); 
+0

당신은 (사용자 활동 페이지에 의미); ? – honeybee

+0

오케이 ive가 그것을 추가했습니다. 처음으로 앱을 실행하면 내 이름을 묻는 메시지가 나타납니다. 열쇠를 입력하면 메뉴 페이지로 이동합니다. 그렇다면 하드웨어 백 버튼을 안드로이드 홈페이지로 가져 가면 내 앱을 다시 시작할 때 여전히 내 이름을 묻습니다. – honeybee

0

, 직접 SavingsGuiderSplashActivity에서 이동하려는 두 번째 ->SavingsGuiderMenuActivity, 건너 뛰기로 SavingsGuiderUserActivity. 즉 당신의 SavingsGuiderSplashActivity의 onAnimationEnd 방법으로 다음에 해당하는 경우는

+0

네가 맞습니다. 하지만 어떻게 확인할 수 있습니까? 공유를 다시 선언하고 스플래시 작업에서 다시 검색하고 if else 문을 수행해야합니까? – honeybee

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(); 
} 
} 

스플래쉬 작업 : 만약 startActivity (의도); 내가 this.finish을 배치 한 직후의 환경 설정 방법을 검색에

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 줄을 사용하여 코드 편집을 요청했을 수 있습니다. 번들 엑스트라 = 새 번들(); extras.putString ("name", strName); intent.putExtras (extras); startActivity (intent); –

+0

사람들이 귀하의 질문에 대답하기 위해 노력한 것에 대해 존경심을 표하십시오. –

+0

Ive 허용! 진드기는 초록색입니다! 당신이 불쾌 해했다면 미안 해요,이 웹 사이트에 새로 온 것이기 때문에! 처음에는 사람들이 저에게 준 모든 대답을 똑딱 똑딱 거리고 나면 나는 "untick 통지"만을 볼 수 있습니다. 그럼 당신이 준 코드가 없기 때문에 나는 지금까지 붙어있을 것입니다. 그리고 녹색 진드기는 내가 맹세코 온통 맹세했다. – honeybee

관련 문제