我有一个scrollView,它会逐行列出程序的状态(onCreate,onPause,onResume等)。每次状态发生变化时都会发生什么,TextView的内容只是被覆盖而不是换行。
我已经尝试了几种方法,但我真的不确定它有什么问题。
这是我的java代码::
int numc= 0;
int nums= 0;
int numr= 0;
int nump= 0;
int numst=0;
int numd= 0;
TextView numCreate;
TextView numStart;
TextView numResume;
TextView numPause;
TextView numStop;
TextView numDestroy;
TextView tCreate;
TextView tStart;
TextView tResume;
TextView tPause;
TextView tStop;
TextView tDestroy;
LinearLayout linear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lifecycle);
numc++;
Log.d("STATE", "onCreate executed");
displayCount();
linear= findViewById(R.id.linearLay);
TextView t1= new TextView(this);
t1.setText("onCreate executed");
linear.addView(t1);
}
@Override
public void onResume() {
super.onResume();
setContentView(R.layout.activity_lifecycle);
numr++;
Log.d("STATE", "onRes executed" + numr);
displayCount();
linear=findViewById(R.id.linearLay);
TextView t2= new TextView(this);
linear.addView(t2);
t2.setText("onResume executed");
}
@Override
public void onPause() {
super.onPause();
setContentView(R.layout.activity_lifecycle);
nump++;
Log.d("STATE", "onPause executed" + nump);
displayCount();
linear=findViewById(R.id.linearLay);
TextView t3= new TextView(this);
linear.addView(t3);
t3.setText("onPause executed");
Log.d("INCREMENT", String.valueOf(linear.getChildCount()));
}
答案 0 :(得分:2)
试试这个
从setContentView(R.layout.activity_lifecycle);
onResume()
onPause()
示例代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lifecycle);
numc++;
Log.d("STATE", "onCreate executed");
displayCount();
TextView t1= new TextView(this);
t1.setText("onCreate executed");
linear.addView(t1);
}
@Override
public void onResume() {
super.onResume();
numr++;
Log.d("STATE", "onRes executed" + numr);
displayCount();
TextView t2= new TextView(this);
linear.addView(t2);
t2.setText("onResume executed");
}
@Override
public void onPause() {
super.onPause();
nump++;
Log.d("STATE", "onPause executed" + nump);
displayCount();
TextView t3= new TextView(this);
linear.addView(t3);
t3.setText("onPause executed");
Log.d("INCREMENT", String.valueOf(linear.getChildCount()));
}
答案 1 :(得分:0)
您应该从setContentView(R.layout.activity_lifecycle);
和onResume()
移除onPause()
。
答案 2 :(得分:0)
在为TextView
设置文字时,请使用"\n
,
例如
t1.setText("onCreate executed"+ "\n");
答案 3 :(得分:0)
从暂停事件中删除setContentView,您可以在LinearLayout
中添加像这样的TextviewTextView htext =new TextView(this);
textView.setText("Any Text");
textView.setId(123);
textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
然后
linearlayout.addView(textView);
答案 4 :(得分:0)
你不需要使用setContentView() 在每种方法中都足以在onCreate中使用它,这个函数会将你的XML膨胀到屏幕
并且足以在findViewById()中声明一个视图,只需在创建时执行
试试这个
public class MainActivity extends AppCompatActivity {
LinearLayout linear;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linear = findViewById(R.id.liner);
textView = new TextView(this);
textView.setText("onCreate executed");
linear.addView(textView);
}
@Override
public void onResume() {
super.onResume();
textView = new TextView(this);
textView.setText("onResume executed");
linear.addView(textView);
}
@Override
public void onPause() {
super.onPause();
textView = new TextView(this);
textView.setText("onPause executed");
linear.addView(textView);
}
}