![](/img/trans.png)
[英]How to get information from an edit text that is entered by the user and move it to another activity as a TextView
[英]Get text of textView from another activity
我試圖從位於類2的textview中獲取文本,以通過按按鈕在類1中使用它。 我通過發送一個Intent來做到這一點,但是我收到了內容錯誤。 那就是我要發送的(從2類到1類):
public static void intent_send(){
Intent i = new Intent();
i.putExtra("number",Integer.parseInt(text_view_current_page.getText().toString()));
class2.startActivity(i);
}
text_view_current_page是靜態TextView,否則它在此void中有錯誤。 在類1中,我通過按下按鈕將其稱為無效:
Class2.intent_send();
Intent i = getIntent();
Bundle b = i.getExtras();
PagerNumber = b.getInt("number");
我在內容定義行中有一個錯誤:
i.putExtra("number",Integer.parseInt(text_view_current_page.getText().toString()));
我應該如何處理此textView才能通過按下按鈕從另一個類中獲取文本? 它應該是靜態的還是應該在收到意圖的類中聲明?
在Android中,活動是完全獨立的組件,因此它們無法直接訪問彼此的東西。 它們具有自己的窗口和視圖層次結構,僅對所有者活動本身專有。
但是,活動之間可以通過兩種方式進行交互。
我對您的情況的建議:
如果您認為第二個活動與第一個活動具有非常緊密的聯系(例如,訪問其視圖層次結構),則可以將第二個活動實現為對話框或片段而不是活動。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.