gpt4 book ai didi

android - 使用 Android LinearLayout 的问题

转载 作者:行者123 更新时间:2023-11-30 02:26:41 25 4
gpt4 key购买 nike

我有一个 LinearLayout,包含一个 EditText 和一个 TextView。我的 ConsoleWindow 在循环中运行,我想在每次迭代中更新 TextView

问题是我可能只初始化一次 EditText(否则它是不可访问的)而且 LinearLayout 可能只初始化一次(否则它会删除 编辑文本).

我不能将 LinearLayoutEditText 放在 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 在 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);
}
}

我该如何解决这个问题?

最佳答案

我想你想在每次迭代时更新它们的颜色/文本。

您无需在每次要修改 View 时都创建一个新 View 。设置您的布局,获取 View ,然后将这些 View 传递到您的循环中。

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");
}
});

关于android - 使用 Android LinearLayout 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27844396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com