gpt4 book ai didi

android - 如何以编程方式关闭 Android 设置菜单?

转载 作者:行者123 更新时间:2023-11-29 01:30:54 27 4
gpt4 key购买 nike

我已经扩展了 Deitel 的“Flag Quiz”(Android for Programmers...App Driven...)(除其他外)允许用户显示一个小旗帜以与给定旗帜进行比较。用户点击 SHOW FLAG,然后获取国家/地区列表,点击一个,然后返回并显示(如下所示)。

enter image description here

我遇到的唯一问题是,在点击所需的国家/地区后,会显示设置 首选项屏幕(见下文)。我猜这是因为 SHOW FLAG 是它的一部分,但我希望用户从列表中选择国家/地区后看到的下一件事就是上面的屏幕。 不需要看到下面的屏幕,所以绝对没有理由必须按后退按钮--如何避免?

enter image description here


仅供引用,这是跟踪和相关代码 fragment (我认为没有理由显示任何 xml):

MainActivity.java:

07-06 17:25:48.305  32269-32269/com.dslomer64.flagquiz W/opOptionsItemSelected? item is <Show flag> -----------------------
07-06 17:25:48.350 32269-32269/com.dslomer64.flagquiz W/Here's where? to get flag
07-06 17:25:48.688 32269-32269/com.dslomer64.flagquiz W/onOptionsItemSelected? Show flag showFlag is true
`FlagActivity.java`:
07-06 17:25:48.768 32269-32269/com.dslomer64.flagquiz W/FlagAct? onCreate
07-06 17:25:51.707 32269-32269/com.dslomer64.flagquiz W/returnToA from FlagAct? Anguilla!!!
`SettingsActivity`:
07-06 17:25:51.751 32269-32269/com.dslomer64.flagquiz W/onCreate? SettingsActivity
(not me)
07-06 17:25:51.783 32269-32269/com.dslomer64.flagquiz W/Resources? Converting to string: TypedValue{t=0x10/d=0x3 a=-1}
07-06 17:25:51.786 32269-32269/com.dslomer64.flagquiz W/Resources? Converting to string: TypedValue{t=0x10/d=0x7d0 a=-1}
`MainActivity.java`:
07-06 17:25:55.554 32269-32269/com.dslomer64.flagquiz W/entering? onActivityResult in MainActivity
07-06 17:25:55.569 32269-32269/com.dslomer64.flagquiz W/onActResult? in Main Act show one flag Anguilla
`QuizFragment.java`:
07-06 17:25:55.591 32269-32269/com.dslomer64.flagquiz W/in showAFlag? country: Anguilla

MainActivity.java,这是保存 Preferences 和选择选项的地方并处理调用 FlagActivity (onActivityResult) 的结果:

   @Override public boolean onOptionsItemSelected(MenuItem item){
Log.w("opOptionsItemSelected", "item is <" + item.getTitle() + "> -----------------------");
Intent preferencesIntent = new Intent(this, SettingsActivity.class);
startActivity(preferencesIntent);
Button getData = (Button)findViewById(R.id.button2);
Intent intent = new Intent(
MainActivity.this,
FlagActivity.class
);
startActivityForResult(intent, 0);

Log.w("onOptionsItemSelected", "" + item.toString() + " showFlag is " + showFlag);
return super.onOptionsItemSelected(item); // to SettingsActivity.onCreate
}

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data ) {
Log.w("entering","onActivityResult in MainActivity");
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == Activity.RESULT_OK){
String enteredData = data.getStringExtra("Data");
Toast.makeText(getApplicationContext(), "Returned " + enteredData, Toast.LENGTH_SHORT).show();;
country = enteredData;
Log.w("onActResult "," in Main Act show one flag " + country);
quizFragment.showAFlag(country);
// ABOVE IS WHERE FLAG IS SHOWN ^^^
}
}

FlagActivity.java,这是处理 ListView 的地方:

    @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flag);
Log.w("FlagAct", " onCreate");
flagAdapter = new ArrayAdapter<String>(this, R.layout.list_item, flagArray);
setListAdapter(flagAdapter);
getListView().setOnItemClickListener(itemClickListener);
}

AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String country = ((TextView) view).getText().toString();
Intent result = new Intent();
result.putExtra("Data", country);
setResult(Activity.RESULT_OK, result);
Toast.makeText(getApplicationContext(), country + "!!!", Toast.LENGTH_SHORT).show();
Log.w("returnToA from FlagAct", country + "!!!");
finish();
}
}; // end itemClickListener

.
SettingsActivity.java:

public class SettingsActivity extends Activity // hosts SettingsFragment in portrait mode
{
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.w("onCreate", "SettingsActivity");
setContentView(R.layout.activity_settings);
}

最佳答案

使用 setOnPreferenceChangeListener() 方法设置一个 Preference.OnPreferenceChangeListener

然后,当国家改变时,调用finish()方法退出你的activity

关于android - 如何以编程方式关闭 Android 设置菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31256503/

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