gpt4 book ai didi

android - 使用 Spinner 开始一个新的 Activity

转载 作者:行者123 更新时间:2023-11-30 04:14:58 27 4
gpt4 key购买 nike

我想知道是否有人知道如何在无需按下按钮的情况下更改微调器的值来启动新 Activity 。
我已经在网上搜索了几个小时,但我找不到任何可以引导我走向正确方向的东西。所以我希望这里有人可以帮助我。

这只是我的标准微调器代码:

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.share);

Button view = (Button) findViewById(R.id.button1);
view.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("net.thinkbin.TUTORIAL1"));
overridePendingTransition(0, 0);
finish();
}
});

Button menu = (Button) findViewById(R.id.buttonhome);
menu.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);

// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Loading...");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.hourglass);

dialog.show();
Thread th = new Thread(new Runnable() {
public void run() {

startActivity(new Intent("net.thinkbin.MENU"));
overridePendingTransition(0, 0);
dialog.dismiss();
finish();

}
});
th.start();
}
});

ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(this, R.array.spinnerfood,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Spinner s = (Spinner) findViewById(R.id.spinner1);
s.setAdapter(adapter);

最佳答案

设置 setOnItemSelectedListener,,,,如果选择某项 onItemSelected 被调用,否则 onNothingSelected 被调用

 s.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String str = (String) arg0.getSelectedItem();

//here print selected value...
System.out.println("String is :: " + str);

//And StartActivity here...

Intent intent = new Intent(YourActivity.this,SecondActivity.class);
startActivity(intent);

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});

关于android - 使用 Spinner 开始一个新的 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10194095/

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