gpt4 book ai didi

java - 将选定的微调器项目值设置为编辑 TextView - Android

转载 作者:行者123 更新时间:2023-12-01 23:04:42 26 4
gpt4 key购买 nike

我是安卓新手。我在微调器中显示字符串,当用户选择一个字符串时,我想使用编辑 TextView 输入选定的微调器字符串值(当用户从​​微调器中选择食物项时,我想使用编辑 TextView 输入食物的值 10)。我已完成微调器项目的设置,但我不知道如何输入选定的微调器值。

这是我的java文件,

public class PaymentsActivity extends Activity implements OnItemSelectedListener {

private Button btnViewExpenses;
private Spinner spinner;
private EditText selectedSpinner;
// array list for spinner adapter
private ArrayList<Category> categoriesList;
ProgressDialog pDialog;

// API urls
// Url to get all categories
private String URL_CATEGORIES = "http://10.0.2.2/category_api/get_categories.php";

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

selectedSpinner = (EditText) findViewById(R.id.categoryItem);
spinner = (Spinner) findViewById(R.id.categoryList);
btnViewExpenses = (Button) findViewById(R.id.btnViewExpenses);

categoriesList = new ArrayList<Category>();

// spinner item select listener
spinner.setOnItemSelectedListener(this);

new GetCategories().execute();

}

/**
* Adding spinner data
* */
private void populateSpinner() {
List<String> lables = new ArrayList<String>();

// txtCategory.setText("");

for (int i = 0; i < categoriesList.size(); i++) {
lables.add(categoriesList.get(i).getName());
}

// Creating adapter for spinner
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);

// Drop down layout style - list view with radio button
spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// attaching data adapter to spinner
spinner.setAdapter(spinnerAdapter);
}

/**
* Async task to get all categories
* */
private class GetCategories extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(PaymentsActivity.this);
pDialog.setMessage("Loading Expenses Information...");
pDialog.setCancelable(false);
pDialog.show();

}

@Override
protected Void doInBackground(Void... arg0) {
ServiceHandler jsonParser = new ServiceHandler();
String json = jsonParser.makeServiceCall(URL_CATEGORIES, ServiceHandler.GET);

Log.e("Response: ", "> " + json);

if (json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
if (jsonObj != null) {
JSONArray categories = jsonObj
.getJSONArray("categories");

for (int i = 0; i < categories.length(); i++) {
JSONObject catObj = (JSONObject) categories.get(i);
Category cat = new Category(catObj.getInt("id"),
catObj.getString("category"));
categoriesList.add(cat);
}
}

} catch (JSONException e) {
e.printStackTrace();
}

} else {
Log.e("JSON Data", "Didn't receive any data from server!");
}

return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
populateSpinner();
}

}


@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(
getApplicationContext(),
parent.getItemAtPosition(position).toString() + " Selected" ,
Toast.LENGTH_LONG).show();

}

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

public void onClickViewExpenses(View view){
Intent viewExpenses = new Intent(this, ExpensesList.class);
startActivity(viewExpenses);
}
}

这是我的 xml 文件,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal">

<TextView
android:id="@+id/accountBalanceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/labelView"
android:layout_marginLeft="26dp"
android:textSize="20dp"
android:text="Account Balance :" />

<Spinner
android:id="@+id/categoryList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />

<Button
android:id="@+id/btnSubmitJobOpenAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/jobOpeningAmount"
android:layout_alignRight="@+id/accBalanced"
android:text="Submit" />

<TextView
android:id="@+id/jobOpeningbalance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/accountBalanceView"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:gravity="center_horizontal"
android:text="Job Opening Amount"
android:textSize="20dp" />

<TextView
android:id="@+id/accBalanced"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/labelView"
android:layout_below="@+id/labelView"
android:layout_marginRight="17dp"
android:text="10000"
android:textColor="#FF0040"
android:textSize="20dp" />

<EditText
android:id="@+id/jobOpeningAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/categoryList"
android:layout_alignLeft="@+id/accountBalanceView"
android:layout_alignRight="@+id/accountBalanceView"
android:layout_marginBottom="22dp"
android:ems="10"
android:inputType="number" >

<requestFocus />
</EditText>

<EditText
android:id="@+id/categoryItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/jobOpeningAmount"
android:layout_alignRight="@+id/jobOpeningAmount"
android:layout_below="@+id/categoryList"
android:layout_marginTop="46dp"
android:ems="10"
android:inputType="number" />

<Button
android:id="@+id/btnSubmitCategoryItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/categoryItem"
android:layout_alignLeft="@+id/btnSubmitJobOpenAmount"
android:text="Submit" />

<Button
android:id="@+id/btnViewExpenses"
android:onClick="onClickViewExpenses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="31dp"
android:text="View Previous Expenses" />

<TextView
android:id="@+id/labelView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:padding="10dp"
android:text="Enter Expenses Here"
android:textSize="25dp"
android:textStyle="bold" />

</RelativeLayout>

请帮我做这件事。

最佳答案

使用这个。

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

String myStr = spinner.getSelectedItem().toString();
selectedSpinner.setText(myStr);

}

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

关于java - 将选定的微调器项目值设置为编辑 TextView - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22985896/

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