2012-06-05 16 views
1

我正在開發一個應用程序,它包含一個聊天,當一個新的消息鍵入一個新的消息時,它將它存儲到內存中的一個文件中。 另外,當我輸入一條新消息時,它會自動滾動到editText的底部(消息所在的位置),並且它在第一次嘗試時工作正常,但是當我再次打開該應用程序並從文件加載歷史記錄,自動滾動不再正常工作,因爲儘管滾動到底部也隱藏了最後一行文本。在android中的scrollTo()的問題

這裏是java文件:

public class ChatActivity extends Activity { 

private static EditText messageHistory; 
// 
private EditText messageInput; 
// 
private Button sendButton; 
// 
private ScrollView scroller; 

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

    //Prevents the keyboard from appearing when not rquested 
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

    ProjectData.chatLogFile = new FileHandler(getApplicationContext(), ProjectData.CHAT_LOG_FILE); 
    ProjectData.chatLogFile.create(); 

    getViews(); 

    getChatLog(); 

    addListennerOnButton(); 
} 

/** 
* Finds all the views in this activity and stores them in global variables 
*/ 
private void getViews() 
{ 
    messageHistory = (EditText) findViewById(R.id.messageHistory); 
    messageInput = (EditText) findViewById(R.id.messageInput); 
    sendButton = (Button) findViewById(R.id.sendButton); 
    scroller = (ScrollView) findViewById(R.id.historyScroller); 
} 

private void getChatLog() 
{ 
    messageHistory.setText(ProjectData.chatLogFile.getData()); 
    scroller.scrollTo(0, messageHistory.getBottom()); 
} 

private void addListennerOnButton() 
{ 
    sendButton.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      if (!messageInput.getText().toString().equals("")) 
      { 
       newMessage(messageInput.getText().toString(), 0); 
       scroller.scrollTo(0, messageHistory.getBottom()); 
       messageInput.setText(""); 
      } 
     } 
    }); 
} 

public static void newMessage (final String msg, int id) 
{ 
    StringBuilder strBuilder = new StringBuilder(); 
    switch (id) { 
     case 0: strBuilder.append("User:\n");break; 
     case 1: strBuilder.append("PC Medic:\n"); 
     default: break; 
    } 

    strBuilder.append(msg + "\n"); 
    messageHistory.append(strBuilder.toString()); 
    messageHistory.refreshDrawableState(); 
    ProjectData.chatLogFile.addData(strBuilder.toString()); 
} 

@Override 
public boolean onCreateOptionsMenu (Menu menu) 
{ 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.options_menu, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) 
{ 
    Intent intent; 
    switch (item.getItemId()) { 
     case R.id.menuFAQ: 
      intent = new Intent(); 
      intent.setClass(getApplicationContext(), FAQActivity.class); 
      startActivity(intent); 
      return true; 
     case R.id.menuConfig: 
      intent = new Intent(); 
      intent.setClass(getApplicationContext(), PrefActivity.class); 
      startActivity(intent); 
      return true; 
     case R.id.menuAbout: 
      intent = new Intent(); 
      intent.setClass(getApplicationContext(), AboutActivity.class); 
      startActivity(intent); 
      return true; 
     case R.id.menuExit: 
      intent = new Intent(); 
      intent.setClass(getApplicationContext(), BackgroundService.class); 
      stopService(intent); 
      setResult(ProjectData.EXIT_CODE); 
      finish(); 
      return true; 
     default: 
      return false; 
    } 
} 
} 

和.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
android:id="@+id/chatMainLayout" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<ImageView 
    android:id="@+id/logoChat" 
    android:layout_width="match_parent" 
    android:layout_height="90dp" 
    android:scaleType="fitStart" 
    android:adjustViewBounds="true" 
    android:src="@drawable/logo_top" 
    android:contentDescription="@string/logo" /> 


<ScrollView 
    android:id="@+id/historyScroller" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:fillViewport="true" 
    android:layout_weight="1" > 

    <EditText 
     android:id="@+id/messageHistory" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:background="@color/black" 
     android:clickable="true" 
     android:editable="false" 
     android:focusable="false" 
     android:gravity="top" 
     android:inputType="textMultiLine" 
     android:textColor="@color/white" /> 
</ScrollView> 

<EditText 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:inputType="none" 
    android:focusable="true" /> 


<LinearLayout 
    android:id="@+id/chatBottomLayout" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 


    <EditText 
     android:id="@+id/messageInput" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:maxHeight="60dp" 
     android:nextFocusLeft="@id/messageInput" 
     android:nextFocusUp="@id/messageInput" 
     android:hint="@string/message_input" 
     android:layout_weight="1" 
     android:textColor="@color/gray" 
     style="@style/textInput" /> 
    <Button 
     android:id="@+id/sendButton" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="3" 
     android:text="@string/send" /> 

</LinearLayout> 

任何人都可以看到,甚至理解這個問題,好嗎?

由於提前,

裏卡多Amendoeira

回答

2

薩那的答案讓我解決了我自己的問題。我認爲你需要允許TextView重新計算窗口邊界,所以試着在文章Handler中包裝你的scrollTo()方法。

見下面我的代碼:

@Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 
    scrollView.post(new Runnable() { 
     @Override 
     public void run() { 
      scrollView.scrollTo(0, 500); 
     } 
    }); 
} 
+1

是的,我實際上能夠解決這個問題(對不起,不張貼它:S)。這是我的代碼: 'chatScrollView。postDelayed(新的Runnable(){ @覆蓋 公共無效的run(){ chatScrollView.fullScroll(ScrollView.FOCUS_DOWN); } },200L);' –

+0

@RicardoAmendoeira這'delay'溶液是唯一的解決方案,爲我工作。謝謝 –

0

這裏的問題是奠定了你的觀點。當你佈局在onCreate方法還沒有完全充氣你的觀點,它在滾動出到第一線工作的正確,因此你可能想要把你的方法getChatLog();

@Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    // TODO Auto-generated method stub 
    super.onWindowFocusChanged(hasFocus); 
    getChatLog(); 
} 

讓我知道,如果它的工作原理不是。

這是一個非常有趣的問題。

+0

謝謝!不幸的是我明天只能測試它,但如果它能正常工作,我會告訴你的! –

+0

我剛測試它,它不適合我....有沒有辦法強制調用onWindowFocusChanged方法?因爲它只在onCreate旁邊被調用....另外我已經把scroller.scrollTo(0,messageHistory.getBottom());在方法以及..任何進一步的幫助將不勝感激! –

+0

messageHistory.getBottom()是否返回底行的位置? – Sana