我正在尝试Android应用程序。我是Java的新手,还在学习。我正在创造一个微调器并试图获得价值。我试图从主页调用该类到另一个类。我收到了祝酒词。现在我想将该值传递给同一页面上的“编辑文本”。有人可以指导我如何在editText中获取值吗?
提前致谢!
my 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) {
}
}
答案 0 :(得分:1)
您在微调器中选择一个项目,它将在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公共静态将解决您的问题
<强> 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 Class
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>
答案 1 :(得分:0)
您可以使用Intent的putExtra()方法传递它。试试这种方式,
在第一项活动中,
Intent intent = new Intent ( FirstAcvity.this, SecondActivity.class );
intent.putExtra ( "TextBox", editText.getText().toString() );
startActivity(intent);
在第二个活动中:
Intent i = getIntent();
tring text = i.getStringExtra ( "TextBox","" );
secondEditText.setText ( text );