gpt4 book ai didi

android - 强制用户在对话框中选择选项

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:42 27 4
gpt4 key购买 nike

第一次启动应用程序时,我想提示用户(通过对话框)选择他们的城市,然后这将保存他们的偏好并将适当的数据加载到 ListView 中。但是,我注意到对话框可以通过后退按钮关闭,或者按对话框外的任何地方,然后触发数据库的空值。

无论如何我可以强制对话框保持打开状态直到用户选择一个选项吗?

我已经实现了 .setCancelable(false) 但这似乎不起作用。我的代码如下,DialogSetup 是我正在使用的内部类。

如有任何帮助/想法,我们将不胜感激。

public class MainActivity extends ListActivity {
private Cursor lines;
private MetroSleepDb db;
private ListAdapter adapter;
public static final String PREFS_NAME = "METROSLEEP_PREFS";

@Override
protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);



chooseCityDialog();




}




public void setAdapters() {

db = new MetroSleepDb(this);
lines = db.getLines(); // you would not typically call this on the main thread
//ListView listView = (ListView) findViewById(R.id.li);
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,
lines,
new String[] {"line_id"},
new int[] {android.R.id.text1}, 0);


getListView().setAdapter(adapter);
getListView().setOnItemClickListener(onAnswerClicked);

}


public String getItem(int pos) {

Cursor c = (Cursor) adapter.getItem(pos);
String value = c.getString(c.getColumnIndex("line_id"));
return value;
}

private OnItemClickListener onAnswerClicked = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {

String line_value = getItem(position);
Intent intent = new Intent(MainActivity.this, ChooseStations.class);
intent.putExtra("line", line_value);
startActivity(intent);
}

};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;

case R.id.menu_settings:
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}

public void chooseCityDialog() {
DialogFragment newFragment = new DialogSetup();
newFragment.show(getFragmentManager(), "citypref");
}

public static final class DialogSetup extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.prompt_choose_city)
.setCancelable(false)
.setItems(R.array.Cities, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//
}
});

return builder.create();
}



}

最佳答案

啊哈!你必须调用setCancelable(false)DialogFragment 上,而不是在 AlertDialog.builder 上。看到这个问题:AlertDialog's setCancelable(false) method not working .

DialogFragment.setCancelable(boolean cancelable) 的文档指出:

Control whether the shown Dialog is cancelable. Use this instead of directly calling Dialog.setCancelable(boolean), because DialogFragment needs to change its behavior based on this.

您只需添加 1 行代码:

public void chooseCityDialog() {
DialogFragment newFragment = new DialogSetup();
newFragment.setCancelable(false);
newFragment.show(getFragmentManager(), "citypref");
}

此外,如果从 Dialog 返回的城市不知何故为 null,那么如果有一个默认城市可以回退,这将是一个很好的安全检查。

关于android - 强制用户在对话框中选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15934215/

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