Android:对话框在单独的线程中获取输入时,等待线程的主线程

我正在Android中编写一个活动,用户可以在其中修改SQL数据库。用户界面由一个EditText(用户在其中输入名称)和一个Seekbar(用户在其中输入用户的吸引力)组成。在下面有很多按钮:添加,编辑,查看,删除。

当用户单击“编辑”按钮时,将显示一个输入对话框,要求用户输入记录号。完成后,将加载该记录。

我遇到的问题是,将显示输入对话框,并且当用户输入记录号时,其余的编辑方法将继续进行,以便在用户输入完输入时-由于该功能已经存在,所以什么也没发生完成了。

为了解决此问题,我决定使用多线程(我没有太多使用经验)。当按下编辑按钮时,主UI线程被阻止(使用wait()-这是因为我不希望用户在输入记录ID时激活UI),并且输入对话框显示在单独的线程。

输入输入后,将通知线程,其余的编辑功能将继续。(下面的代码)。

我知道通常不应该阻止UI线程,但是我认为在这种情况下还可以,因为我不希望它接受任何用户输入。

谢谢你的帮助。

public class Attractivometer extends Activity implements OnClickListener {

private Button buttonAddRecord, buttonEditRecord, buttonSaveChanges;

private Button buttonDeleteRecord, buttonViewRecord;

private EditText fieldName;

private SeekBar seekbarAttractiveness;

private String inputFromInputDialog=null;

private Thread inputThread;

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.Attractivometer);

buttonAddRecord = (Button) findViewById(R.id.buttonAddRecord);

buttonSaveChanges = (Button) findViewById(R.id.buttonSaveChanges);

buttonEditRecord = (Button) findViewById(R.id.buttonEditRecord);

buttonDeleteRecord = (Button) findViewById(R.id.buttonDeleteRecord);

buttonViewRecord = (Button) findViewById(R.id.buttonViewRecord);

fieldName = (EditText) findViewById(R.id.fieldName);

seekbarAttractiveness = (SeekBar) findViewById(R.id.seekbarAttractiveness);

buttonAddRecord.setOnClickListener(this);

buttonSaveChanges.setOnClickListener(this);

buttonEditRecord.setOnClickListener(this);

buttonDeleteRecord.setOnClickListener(this);

buttonViewRecord.setOnClickListener(this);

}

public void onClick(View clickedItem)

{

switch(clickedItem.getId())

{

case R.id.buttonAddRecord:

//.....

break;

case R.id.buttonSaveChanges:

//...

break;

case R.id.buttonEditRecord:

inputThread = new Thread(new Runnable(){

public void run()

{

showInputDialog("Enter Record ID", InputType.TYPE_CLASS_NUMBER);

}

});

inputThread.start();

try {

Thread.currentThread().wait();

} catch (InterruptedException e) {

Log.e("Attractivometer","Main Thread interrupted while waiting");

e.printStackTrace();

}

try {

inputThread.join();

} catch (InterruptedException e) {

Log.e("Attractivometer","Input Thread interrupted while joining");

e.printStackTrace();

}

int recordId = Integer.parseInt(inputFromInputDialog);

if(recordId!=null)

{

AttractivometerSQLHandler AttractivometerDatabaseHandler = new AttractivometerSQLHandler(this);

AttractivometerDatabaseHandler.openDatabase();

String recordName = AttractivometerDatabaseHandler.getName(recordId);

String recordAttractiveness = AttractivometerDatabaseHandler.getAttractiveness(recordId);

if(recordName==null || recordAttractiveness==null )

{

//no record found.

Toast.makeText(this, "No record with that ID found", Toast.LENGTH_SHORT).show();

}else

{

fieldName.setText(recordName);

seekbarAttractiveness.setProgress( Integer.parseInt(recordAttractiveness) );

recordIsOpen(true);

}

AttractivometerDatabaseHandler.closeDatabase();

}else

//No input.

recordIsOpen(false);

break;

case R.id.buttonDeleteRecord:

//...

break;

case R.id.buttonViewRecord:

//....

}

}

private void showInputDialog(String prompt, int inputType)

{

AlertDialog.Builder inputDialog = new AlertDialog.Builder(this);

inputDialog.setTitle("Record No.");

final EditText fieldInput = new EditText(this);

fieldInput.setInputType(inputType);

fieldInput.setHint(prompt);

inputDialog.setView(fieldInput);

inputDialog.setPositiveButton("OK", new DialogInterface.OnClickListener()

{

public void onClick(DialogInterface arg0, int arg1)

{

inputFromInputDialog = fieldInput.getText().toString();

inputThread.notify();

}

});

inputDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener()

{

public void onClick(DialogInterface arg0, int arg1)

{

inputFromInputDialog = null;

inputThread.notify();

}

});

inputDialog.show();

}

}

回答:

不不不!不要阻塞UI线程。系统将引发“应用程序无响应”错误。另外,请勿尝试通过非UI线程与用户进行交互。

当用户单击“编辑”时,请勿启动编辑方法。只需弹出一个对话框即可收集所需的信息。DialogInterface.OnClickListener在肯定的按钮上添加a

,然后(现在有了所需的信息)从那里开始编辑方法。

有关更多信息,请参见指南主题对话框。

以上是 Android:对话框在单独的线程中获取输入时,等待线程的主线程 的全部内容, 来源链接: utcz.com/qa/401310.html

回到顶部