2013-01-10 15 views
-3

我遇到了另一個棘手的小問題。android公用文本框

我正在用選項卡編寫應用程序,但我希望能夠從任何選項卡接收文本數據的屏幕頂部有一個文本框(EditText)。由於每個選項卡都有自己的活動和佈局,這很難實現。

我希望能夠使用:

editText1.setText("Hello World");//sample text 
從任何選項卡/活動

有誰知道如何從一個佈局公開一個文本框,並能夠接收文本?

我使用TabActivity,是的我知道它已被棄用,但因爲這是我的第一個應用程序與標籤,它比Fragments更容易學習。我會在下次嘗試他們,除非他們是我的問題的答案,在這種情況下,我有很多重新編碼!

好的,新的部分。

package com.epsilonitsystems.epecsandroid; 

import android.app.TabActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.EditText; 
import android.widget.TabHost; 
import android.widget.TextView; 
import android.widget.TabHost.TabSpec; 
import android.view.Menu; 
import android.speech.tts.TextToSpeech; 

public class MainActivity extends TabActivity { 

public EditText editText1; 


@Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    editText1 = (EditText) findViewById(R.id.editText1); 
    TabHost tabHost = getTabHost(); 

    // Tab for Core 
    TabSpec corespec = tabHost.newTabSpec("Core"); 
    corespec.setIndicator("", getResources().getDrawable(R.drawable.ic_i)); 
    Intent coreIntent = new Intent(this, CoreActivity.class); 
    corespec.setContent(coreIntent); 

    // Tab for Drink 
    TabSpec drinkspec = tabHost.newTabSpec("Drink"); 
    drinkspec.setIndicator("", getResources().getDrawable(R.drawable.ic_drink)); 
    Intent drinkIntent = new Intent(this, DrinkActivity.class); 
    drinkspec.setContent(drinkIntent); 


    // Adding all TabSpec to TabHost 
    tabHost.addTab(corespec); // Adding Core tab  
    tabHost.addTab(drinkspec); // Adding Drink tab 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

}

這是最主要的活動,我將只顯示核心活動,他們都將是一樣的,當我得到它的工作。

package com.epsilonitsystems.epecsandroid; 


import android.app.Activity; 
import android.os.Bundle; 
import android.speech.tts.TextToSpeech; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageButton; 
import android.widget.TextView; 
import android.speech.tts.TextToSpeech; 

public class CoreActivity extends Activity { 

private TextToSpeech mTts; 
// This code can be any value you want, its just a checksum. 
private static final int MY_DATA_CHECK_CODE = 1234; 

EditText editText1; 
String Spch,Str; 
ImageButton imageButton1,imageButton2; 

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

    //button1 = (Button) findViewById(R.id.button1); 
    editText1 = (EditText) findViewById(R.id.editText1); 
    mTts = new TextToSpeech(this, null); 

} 

public void onimageButton1Click(View view) { 
//mTts.speak("I",TextToSpeech.QUEUE_FLUSH,null); 
//Spch=editText1.toString(); 
//Spch=Spch+"I "; 

    editText1.setText("Hello World"); 
} 


}//Activity End 

我無法發佈屏幕截圖,因爲我還是新用戶,抱歉。

有什麼想法嗎?

回答

0

Gary,

你在找什麼可能是如何共享不同的活動之間的數據。 您可以通過幾種方式來完成此操作,但最乾淨的方法是使用Intent,然後開始新的活動。 有了這個,你可以通過Intent.putXXXXX方法設置額外的數據。 你可以使用Intent.putExtra(「my_private_key」,mTextVar);並使用this.getIntent()。getStringExtra(「my_private_key」);

ps。雖然看起來好像是以TabActivity開始的,但實際上讓自己變得更加困難,因爲開始糟糕的開始和學習不應該再使用的課程。我建議你選擇一些好的片段教程,一旦正確解釋就會很容易。你應該看看每個例子http://www.vogella.com/android.html

+0

嗨Stefan,我會帶上關於TabActivity的建議,謝謝。至於使用Intent,我開始使用Activity,但我認爲這不會解決我的問題。當用戶按下任何選項卡中的按鈕時,我需要能夠向文本框添加文本。我正在爲孩子們編寫一個交流計劃,他們按下帶有符號的按鈕來建立一個句子。不同的詞語被分類在不同的標籤中。 – Gary

+0

Gary, 您的代碼的某些部分和/或應用的屏幕截圖可能會讓事情變得更加清晰。據我的理解,從你的評論你只需要在屏幕頂部的一堆按鈕,將文本附加到文本字段。爲什麼不只是使用(風格)按鈕呢? –

+0

沒問題,我們走吧, – Gary