gpt4 book ai didi

android - 我如何从微调器 onitemSelected 方法中调用 Intent

转载 作者:行者123 更新时间:2023-11-29 16:20:20 24 4
gpt4 key购买 nike

我的 Android 应用程序中有一个微调器控件,我希望用户从微调器中选择一个选项。选择该选项后,可以将用户重定向到另一个 Intent 。我的问题是如何从微调器的 onitemselcted 方法调用 Intent ??。

或者我正在绞尽脑汁还有其他方法可以做到这一点。在进入下一页之前,我需要用户先从下拉列表中设置选项。

If i put startactivity(intent) in my onitemselected method i get this error The method startActivity(Intent) is undefined for the type MyOnItemSelectedListener (Myonitemselectedlistner is my class which implements OnItemSelectedListener)

这是我的 onitemslectedlistner 代码

    class MyOnItemSelectedListener implements OnItemSelectedListener {

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

Context ctx = view.getContext();

SharedPreferences myPrefs = ctx.getSharedPreferences("hello", android.content.Context.MODE_WORLD_READABLE );

SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("city", parent.getItemAtPosition(pos).toString());
prefsEditor.commit();
Intent intent = new Intent();
intent.setClass(ctx, MainActivity.class);
startActivity(intent);


}

public void onNothingSelected(AdapterView parent) {

//do nithong
}
}

最佳答案

尝试做类似的事情:

YourParentClassName.this.startActivity(intent);

这是经过全面测试的实现...这有效:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if (position == 1){
Intent intent = new Intent(MyActivity.this, AnotherActivity.class);
MyActivity.this.startActivity(intent);
}
}

public void onNothingSelected(AdapterView<?> parentView) {
// To do ...
}

});

关于android - 我如何从微调器 onitemSelected 方法中调用 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7404395/

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