gpt4 book ai didi

java - Android从其他线程获取EditText内容

转载 作者:行者123 更新时间:2023-12-01 10:15:57 25 4
gpt4 key购买 nike

在我的问题中,我有我的 MainActivity ,带有 TextViewEditText ,以及一个方法,该方法应将消息发送到 TextView并收到 EditText 的内容。问题是从 EditText 接收文本,并使该方法等待用户输入

我将尝试发布我的裁剪代码,以便您了解我想要实现的目标。

public class MainActivity extends Activity {

public class MonitorObject{
}
final MonitorObject mSync = new MonitorObject();
private TextView mConsoleOut;
private EditText mInputLine;
public String inputString;
public synchronized String getInputString(String value){this.inputString = value; return inputString;}

IOHandler IOhandler;

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

mConsoleOut = (TextView) findViewById(R.id.consoleOut);
mInputLine = (EditText) findViewById(R.id.actionInField);


EditText editText = (EditText) findViewById(R.id.actionInField);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
synchronized (mSync){
mSync.notify();
}
return true;
}
});
GameCycle gameCycle = new GameCycle();
Thread gameloop = new Thread(gameCycle);
gameloop.start();
}

public class GameCycle implements Runnable{
public void run(){
game();
}
}

public void game(){
findViewById(R.id.actionInField);
Integer time=0;
PlayerClass p1 = new PlayerClass(1,4);
TileClass tile = new TileClass.Forest();
Integer gamestate=0;


while(gamestate==0){
time++;
tile.initialize_tilesettings(time, p1);
tile.passed=true;
List actionResponse=new ArrayList(Arrays.asList("repeat", "type", "returnval"));

while(gamestate==0 && !actionResponse.get(1).equals("tileChange")){

actionResponse=Arrays.asList("repeat", "type", "returnval");
Boolean tileActions = true;

while(!actionResponse.get(0).equals("continue")){
//actions to be repeated

String exe=null;

tileActions=true;

List<String> params = new ArrayList<>();

<<<<<print>>>>>("\nWhat will you do?");

synchronized (mSync){
try {
mSync.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

<<<<<getinput>>>>>String inp=returnInputString();
String[] splitIn=inp.split(" ");


for(String text:splitIn){
params.add(text.toLowerCase());
}

for(ActionClass action:p1.getActions()){
if(action.method.contains(params.get(0))){
exe=action.method;
}
}

if(exe==null){
<<<<<print>>>>>("Not a valid action");

actionResponse=Arrays.asList("repeat","","");
}
else{
try{
actionResponse=p1.excecFunctionByTag(exe, Arrays.asList(p1, tile, params));
if(actionResponse==null){
throw new RuntimeException();
}
}
catch (Exception e){
for(ActionClass action:p1.getActions()){
if(exe.equals(action.method)){
String actionEnt=action.actionEntry;
<<<<<print>>>>>("\nInvalid parameters, try: "+actionEnt);
actionResponse=Arrays.asList("repeat","","");
}
}
}
return;
}


}
}
if(gamestate==1){
<<<<<print>>>>>('You died! Game over!\n\n<-<-< New Game >->->\n\n')
game();
}
else if(gamestate==2){
<<<<<print>>>>>("You have defeated the boss! Behind the fallen enemy you can see a path. You follow it and find a small village. You are safe!\n\n<-<-< New Game >->->\n\n")
game();
}
}
}

我已经用 <<<<< >>>>> 标记了所需的输入/输出方法.

请怜悯我的java技能,因为这是我用java编写的第一个大型项目,整个结构都是从python转换而来的。

再次,非常感谢所有帮助。

最佳答案

问题很简单,通常线程不会通知用户界面或主线程。为了解决这个问题,android 有 AsyncTask。您可以将 AsyncTask 用作:

  1. 在 onPreExecute() 方法中启动 TextView 和 EditText。
  2. 发送或调用您的方法以在 doInBackground() 方法中发送您想要的任何文本。
  3. 然后您将在 onPostExecute() 方法中获得结果。在这里您可以将 Text() 设置为您的 EditText。

注意:AsyncTask 是一个联系主 ui 线程的线程,它有一个 onPostExecute() 方法。

为了更好地理解 AsyncTask,请查看此链接: http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

关于java - Android从其他线程获取EditText内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35888260/

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