gpt4 book ai didi

java - 在 Android 应用程序中从 strings.xml 分配值时无法获取选定的微调器值

转载 作者:行者123 更新时间:2023-11-30 02:44:30 26 4
gpt4 key购买 nike

android application populate valuesspinner 中,我使用了 values\strings.xml 中的值

 <string-array name="myList">
<item>first</item>
<item>second</item>
</string-array>

layout\input_box.xml

 <Spinner
android:id="@+id/spinner_problems"
android:entries="@array/myList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

以上代码将从 myList 填充微调器值

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {

spin_problem = (Spinner)findViewById((R.id.spinner_problems));
spin_problem.setOnItemSelectedListener(new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> adapter, View view1, int pos, long id)
{
spin_problem_val= adapter.getItemAtPosition(pos).toString();
}

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

}
});

//thread run
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
try {
showInputDialog("Any Complaints?");
} catch (JSONException e) {
e.printStackTrace();
}
}}, 8000); // 3000 milliseconds
//end thread

}

showInputDailog 方法代码//我的文本框和微调器在这个方法中

public void showInputDialog(final String textparam) throws JSONException {      
try{
// get prompts.xml view to show complaint popup
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setView(promptView);
if(textparam.toString() == "Feedback"){alertDialogBuilder.setTitle("Feedback");}
else{alertDialogBuilder.setTitle("Any Complaints?");}

//Get all dialog box values
final EditText name = (EditText) promptView
.findViewById(R.id.name);
final EditText mobno = (EditText) promptView
.findViewById(R.id.mobno);
final EditText complaint = (EditText) promptView
.findViewById(R.id.complaint);



// final Spinner problems = (Spinner)findViewById((R.id.spinner_problems));
// final Spinner area = (Spinner)findViewById((R.id.spinner_area));

// final String aa = problems.getSelectedItem().toString();
// final String vvv = area.getSelectedItem().toString();

//这里我需要获得微调器值以进一步处理...

最佳答案

要从微调器获取值,请将 onItemSelectedListener 添加到微调器。像这样:

     String value;
final Spinner problems = (Spinner)findViewById((R.id.spinner_problems));
problems.setOnItemSelectedListener(new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> adapter, View view1, int pos, long id)
{
value= adapter.getItemAtPosition(pos).toString();
}

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

}
});

编辑:

-有关更多信息,请查看this , this , 和 this .

编辑 2:

  • 如果您的微调器在您的 main_activity 中,请将代码添加到您的 onCreate 方法中,并将 value 作为全局变量。

  • 如果您希望在 alertDialog 中使用微调器,请在 showInputDialog 方法中添加代码

关于java - 在 Android 应用程序中从 strings.xml 分配值时无法获取选定的微调器值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25312407/

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