2016-01-26 73 views
0

我想要一個佈局,顯示當前使用哪個函數的消息。如何顯示在android中使用textview使用哪個函數?

在運行下面的代碼時,它只是向我展示了最新的TextView,而不是所有的TextView的使用。

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     LinearLayout linear = new LinearLayout(this); 
     TextView tv=new TextView(this); 
     tv.setText("Created"); 
     linear.addView(tv); 
     setContentView(linear); 
    } 

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

     LinearLayout linear = new LinearLayout(this); 
     TextView tv=new TextView(this); 
     tv.setText("Started"); 
     linear.addView(tv); 
     setContentView(linear); } 


    @Override 
    protected void onResume() 
    { 
     super.onResume(); 
     LinearLayout linear = new LinearLayout(this); 
     TextView tv=new TextView(this); 
     tv.setText("Resumed"); 
     linear.addView(tv); 
     setContentView(linear); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
    } 

    @Override 
    protected void onRestart() { 
     super.onRestart(); 
    } 

} 
+2

爲什麼你一直在改變setContentView? – Brendon

回答

0

您在每個功能中都使用setContentViewsetContentView取代之前定義的任何Activity視圖。您想在每個功能上添加TextView文字。因此,您應該在onCreate中設置內容視圖,然後獲取視圖並在您添加到onCreate中的LinearLayout中添加TextView。創建全局線性佈局,然後使用,就像下面一樣

LinearLayout linear; 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
linear = new LinearLayout(this); 
    TextView tv=new TextView(this); 
    tv.setText("Created"); 
    linear.addView(tv); 
    setContentView(linear); 
} 

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

    TextView tv=new TextView(this); 
    tv.setText("Started"); 
    linear.addView(tv); 
} 


@Override 
protected void onResume() 
{ 
    super.onResume(); 
    TextView tv=new TextView(this); 
    tv.setText("Resumed"); 
    linear.addView(tv); 
} 

希望這會有所幫助。

+0

它工作。謝謝。 –

0

活動類,

public class MainActivity extends AppCompatActivity { 


TextView tvStart, tvCreate, tvResume; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    tvCreate = (TextView)findViewById(R.id.tvCreate); 
    tvStart = (TextView)findViewById(R.id.tvStart); 
    tvResume = (TextView)findViewById(R.id.tvResume); 



    tvCreate.setText("Created"); 

} 


@Override 
protected void onStart() { 
    super.onStart();Started 

    tvStart.setText("Started"); 
} 


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

    tvResume.setText("Resumed"); 
} 
} 

XML對於這一點,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.technobd.stack.MainActivity" 
tools:showIn="@layout/activity_main" 
android:orientation="vertical"> 

<TextView 
    android:id="@+id/tvCreate" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!" /> 

<TextView 
    android:id="@+id/tvStart" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!" /> 

<TextView 
    android:id="@+id/tvResume" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!" /> 

您還可以添加textviews動態像, Activity類會,

public class MainActivity extends AppCompatActivity { 


LinearLayout llTextViews; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    llTextViews = (LinearLayout)findViewById(R.id.llTextViews); 


    TextView tvCreate = new TextView(MainActivity.this); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    tvCreate.setLayoutParams(params); 
    tvCreate.setText("Created"); 

    llTextViews.addView(tvCreate); 

} 


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

    TextView tvStart = new TextView(MainActivity.this); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    tvStart.setLayoutParams(params); 
    tvStart.setText("Started"); 

    llTextViews.addView(tvStart); 

} 


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

    TextView tvResume = new TextView(MainActivity.this); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    tvResume.setLayoutParams(params); 
    tvResume.setText("Resumed"); 

    llTextViews.addView(tvResume); 
} 
} 

和xml佈局是,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.technobd.stack.MainActivity" 
tools:showIn="@layout/activity_main" 
android:orientation="vertical"> 

<LinearLayout 
    android:id="@+id/llTextViews" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:orientation="vertical"></LinearLayout> 
</LinearLayout> 
+0

我剛剛開始在android中編碼,所以我無法獲取FindViewById的上下文。 –

+0

其「findViewById」不是「FindViewById」 記住Java區分大小寫。 在活動類中,您無需執行任何操作,只需鍵入代碼即可。對於初學者來說, – Exigente05

+0

想要一些容易理解的東西。 –

相關問題