我需要一段android代码,要用到List view和spinner,这段代码可以让用户填写自己的个人信息,最后弹窗展示
时间: 2023-05-26 13:02:14 浏览: 167
Android代码-nice-spinner
以下是一段android代码,使用了List view和Spinner,可以让用户填写自己的个人信息,并在最后弹窗展示:
MainActivity.java
```java
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private ArrayAdapter<String> adapter;
private String[] items = {"Name", "Age", "Gender", "Country"};
private String[] values = {"", "", "", ""};
private EditText editTextName;
private EditText editTextAge;
private Spinner spinnerGender;
private ArrayAdapter<String> spinnerAdapter;
private String[] genders = {"Male", "Female", "Other"};
private Spinner spinnerCountry;
private ArrayAdapter<String> countryAdapter;
private String[] countries = {"China", "USA", "Canada", "UK", "France", "Germany"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
editTextName = new EditText(this);
editTextName.setHint("Name");
editTextName.setMaxLines(1);
editTextAge = new EditText(this);
editTextAge.setHint("Age");
editTextAge.setMaxLines(1);
spinnerGender = new Spinner(this);
spinnerAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, genders);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerGender.setAdapter(spinnerAdapter);
spinnerCountry = new Spinner(this);
countryAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, countries);
countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCountry.setAdapter(countryAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
showDialog(editTextName, position);
break;
case 1:
showDialog(editTextAge, position);
break;
case 2:
showDialog(spinnerGender, position);
break;
case 3:
showDialog(spinnerCountry, position);
break;
}
}
});
}
private void showDialog(final View view, final int position) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(items[position]);
builder.setView(view);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
switch (position) {
case 0:
values[position] = editTextName.getText().toString();
break;
case 1:
values[position] = editTextAge.getText().toString();
break;
case 2:
values[position] = spinnerGender.getSelectedItem().toString();
break;
case 3:
values[position] = spinnerCountry.getSelectedItem().toString();
break;
}
Log.d("MainActivity", "Values: " + values[0] + " " + values[1] + " " + values[2] + " " + values[3]);
}
});
builder.setNegativeButton("Cancel", null);
builder.show();
}
public void showResult(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Personal Information");
builder.setMessage("Name: " + values[0] + "\nAge: " + values[1] + "\nGender: " + values[2] + "\nCountry: " + values[3]);
builder.setPositiveButton("OK", null);
builder.show();
}
}
```
activity_main.xml
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.listview.MainActivit">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/button"
android:layout_alignParentTop="true"
android:dividerHeight="1dp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Show Result"
android:onClick="showResult" />
</RelativeLayout>
```
运行效果:
![image-20210717165105865](https://cdn.jsdelivr.net/gh/Yousazoe/picgo-repo//blog/images/image-20210717165105865.png)
阅读全文