gpt4 book ai didi

java - 从其他类获取 spinner 值并将其分配给 editText

转载 作者:行者123 更新时间:2023-12-01 09:35:46 25 4
gpt4 key购买 nike

我正在尝试 Android 应用程序。我对 Java 还很陌生,并且仍在学习中。我正在创建一个旋转器并试图获得值(value)。我试图将类(class)从主页调用到另一个类(class)。我收到 toast 消息。现在我想将该值传递给同一页面上的编辑文本。有人可以指导我如何获取 editText 中的值吗?

提前致谢!

我的activity_main_page.xml

<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/token_spinner"
android:layout_above="@+id/textView5"
android:layout_alignStart="@+id/textView3" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="@+id/editText_order"
android:layout_alignEnd="@+id/textView2"
android:layout_alignStart="@+id/token_spinner" />

我的MainPage.Java

public class MainPage extends AppCompatActivity {

Spinner spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);

//Drop Down List Selction of Token
Spinner spinner = (Spinner) findViewById(R.id.token_spinner);

ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.plan_array, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(new TokenSpinner());
}

}

TokenSpinner.java

public class TokenSpinner implements AdapterView.OnItemSelectedListener {

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String token = adapterView.getItemAtPosition(i).toString();

if (plan.equals("154")) {
Toast.makeText(adapterView.getContext(), "Token " + token, Toast.LENGTH_LONG).show();

//I am gettin Toast here
//do something and assign to editText ordervalue

}
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
}

最佳答案

您在微调器中选择一个项目,它将在 EditText 中设置..

EditText textField =(EditText)findViewById(R.id.item_edittext);
Spinner spineer;
spinner= (Spinner)findViewById(R.id.spinner_item);
spineer.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l{
String selectedItemInSpinner=spinner.getSelectedItem().toString();
textField.setText(selectedItemInSpinner);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {}
});

编辑代码

好吧,我不知道这是好方法还是坏方法,但是将 EditText 设为公共(public)静态将解决您的问题

Mainactivity.java

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity {
public static String selectedItemInSpinner="";
Spinner spinner;
public static EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Drop Down List Selction of Token
spinner = (Spinner) findViewById(R.id.token_spinner);
editText = (EditText) findViewById(R.id.editText_order);

ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.plan_array, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(new TokenSpinner());
editText.setText(selectedItemInSpinner);
}}

TokenSpinner 类

public class TokenSpinner implements android.widget.AdapterView.OnItemSelectedListener {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
MainActivity.selectedItemInSpinner=parent.getSelectedItem().toString();
MainActivity.editText.setText(MainActivity.selectedItemInSpinner);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/token_spinner"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="@+id/editText_order"
/>
</LinearLayout>

关于java - 从其他类获取 spinner 值并将其分配给 editText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38953127/

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