使用 Android LinearLayout 的问题
Issues Using Android LinearLayout
我有一个 LinearLayout
,包含一个 EditText
和一个 TextView
。我的 ConsoleWindow
在循环中运行,我想在每次迭代中更新 TextView
。
问题是我只能初始化 EditText
一次(否则无法访问)并且 LinearLayout
只能初始化一次(否则它会删除 EditText
).
我不能将 LinearLayout
和 EditText
放在 if 语句中:
if (firstRun) {
// initialize LinearLayout and EditText
firstRun = false;
}
// TEXTVIEW
TextView tv = new TextView(getApplicationContext())
tv.setText(dataStringTot);
layout.addView(tv); // "Qualifier must be an Expression."
因为 IDE Returns 在 layout.addView(tv);
: "Qualifier must be an expression."
我的代码:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getInetContent();
}
getInetContent() { // would be a thread
// getting data...
Bundle b = new Bundle();
b.putString("StringNMEA", NMEA);
Message m = mhandler.obtainMessage();
m.setData(b);
mhandler.sendMessage(m);
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
String dataString = "";
Bundle bundle = msg.getData();
if (bundle.containsKey("display")) {
ConsoleWindow(dataString);
}
}
}
private void ConsoleWindow(String dataString) {
// LINEAR LAYOUT
LinearLayout layout = new LinearLayout(this);
setContentView(layout);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(Color.parseColor("#000000")); // black
// EDITTEXT
EditText et = new EditText(getApplicationContext());
et.setHint("Enter Command");
layout.addView(et);
// TEXTVIEW
TextView tv = new TextView(getApplicationContext());
tv.setText(dataStringTot);
layout.addView(tv);
}
}
我的带有 if 语句的代码:
public boolean firstRun = true;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getInetContent();
}
getInetContent() { // would be a thread
// getting data...
Bundle b = new Bundle();
b.putString("StringNMEA", NMEA);
Message m = mhandler.obtainMessage();
m.setData(b);
mhandler.sendMessage(m);
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
String dataString = "";
Bundle bundle = msg.getData();
if (bundle.containsKey("display")) {
ConsoleWindow(dataString);
}
}
}
private void ConsoleWindow(String dataString) {
if (firstRun) {
// initialize LinearLayout and EditText:
// LINEAR LAYOUT
LinearLayout layout = new LinearLayout(this);
setContentView(layout);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(Color.parseColor("#000000")); // black
// EDITTEXT
EditText et = new EditText(getApplicationContext());
et.setHint("Enter Command");
layout.addView(et);
firstRun = false;
}
// TEXTVIEW
TextView tv = new TextView(getApplicationContext());
tv.setText(dataStringTot);
layout.addView(tv);
}
}
我该如何解决这个问题?
我想你想在每次迭代中更新它们的 colours/text。
您不需要每次修改视图时都创建一个新的视图。设置您的布局,获取视图,然后将这些视图传递到您的循环中。
LinearLayout layout = new LinearLayout(this);
EditText et = new EditText(this);
TextView tv = new TextView(this);
layout.addView(et);
layout.addView(tv);
setContentView(layout);
et.setHint("Enter Command");
tv.setText(dataStringTot);
startLoop(et, tv);
public void startLoop(final EditText et, final TextView tv) {
...
}
如果您的循环是某种不在主线程上的 运行 线程,您可能需要将其包装在 ui 线程上的 运行 中。
runOnUiThread(new Runnable()
{
public void run()
{
Log.v("mainActivity", "test");
}
});
我有一个 LinearLayout
,包含一个 EditText
和一个 TextView
。我的 ConsoleWindow
在循环中运行,我想在每次迭代中更新 TextView
。
问题是我只能初始化 EditText
一次(否则无法访问)并且 LinearLayout
只能初始化一次(否则它会删除 EditText
).
我不能将 LinearLayout
和 EditText
放在 if 语句中:
if (firstRun) {
// initialize LinearLayout and EditText
firstRun = false;
}
// TEXTVIEW
TextView tv = new TextView(getApplicationContext())
tv.setText(dataStringTot);
layout.addView(tv); // "Qualifier must be an Expression."
因为 IDE Returns 在 layout.addView(tv);
: "Qualifier must be an expression."
我的代码:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getInetContent();
}
getInetContent() { // would be a thread
// getting data...
Bundle b = new Bundle();
b.putString("StringNMEA", NMEA);
Message m = mhandler.obtainMessage();
m.setData(b);
mhandler.sendMessage(m);
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
String dataString = "";
Bundle bundle = msg.getData();
if (bundle.containsKey("display")) {
ConsoleWindow(dataString);
}
}
}
private void ConsoleWindow(String dataString) {
// LINEAR LAYOUT
LinearLayout layout = new LinearLayout(this);
setContentView(layout);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(Color.parseColor("#000000")); // black
// EDITTEXT
EditText et = new EditText(getApplicationContext());
et.setHint("Enter Command");
layout.addView(et);
// TEXTVIEW
TextView tv = new TextView(getApplicationContext());
tv.setText(dataStringTot);
layout.addView(tv);
}
}
我的带有 if 语句的代码:
public boolean firstRun = true;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getInetContent();
}
getInetContent() { // would be a thread
// getting data...
Bundle b = new Bundle();
b.putString("StringNMEA", NMEA);
Message m = mhandler.obtainMessage();
m.setData(b);
mhandler.sendMessage(m);
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
String dataString = "";
Bundle bundle = msg.getData();
if (bundle.containsKey("display")) {
ConsoleWindow(dataString);
}
}
}
private void ConsoleWindow(String dataString) {
if (firstRun) {
// initialize LinearLayout and EditText:
// LINEAR LAYOUT
LinearLayout layout = new LinearLayout(this);
setContentView(layout);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(Color.parseColor("#000000")); // black
// EDITTEXT
EditText et = new EditText(getApplicationContext());
et.setHint("Enter Command");
layout.addView(et);
firstRun = false;
}
// TEXTVIEW
TextView tv = new TextView(getApplicationContext());
tv.setText(dataStringTot);
layout.addView(tv);
}
}
我该如何解决这个问题?
我想你想在每次迭代中更新它们的 colours/text。
您不需要每次修改视图时都创建一个新的视图。设置您的布局,获取视图,然后将这些视图传递到您的循环中。
LinearLayout layout = new LinearLayout(this);
EditText et = new EditText(this);
TextView tv = new TextView(this);
layout.addView(et);
layout.addView(tv);
setContentView(layout);
et.setHint("Enter Command");
tv.setText(dataStringTot);
startLoop(et, tv);
public void startLoop(final EditText et, final TextView tv) {
...
}
如果您的循环是某种不在主线程上的 运行 线程,您可能需要将其包装在 ui 线程上的 运行 中。
runOnUiThread(new Runnable()
{
public void run()
{
Log.v("mainActivity", "test");
}
});