android开发要求:界面上方,左边一个文本框,右边一个按钮1,界面往下还有一个按钮2。单击按钮1,显示一个列表对话框,单击该对话框上的任一个选项,对话框消失,在对话框上选择的选项内容显示在文本框中;单击按钮2,进入界面2,并将界面1上文本框的内容显示在界面2上。代码
时间: 2024-03-16 19:42:43 浏览: 54
好的,以下是实现该功能的代码示例,仅供参考:
MainActivity.java:
```
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button button1, button2;
private String[] items = {"选项1", "选项2", "选项3"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.edit_text);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showListDialog();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("text", editText.getText().toString());
startActivity(intent);
}
});
}
private void showListDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
editText.setText(items[which]);
}
});
builder.show();
}
}
```
activity_main.xml:
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="80dp"
android:hint="请输入内容" />
<Button
android:id="@+id/button1"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="选择"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="进入界面2"
android:layout_below="@id/edit_text"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
</RelativeLayout>
```
SecondActivity.java:
```
public class SecondActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
textView = findViewById(R.id.text_view);
Intent intent = getIntent();
String text = intent.getStringExtra("text");
textView.setText(text);
}
}
```
activity_second.xml:
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:textSize="20sp"/>
</RelativeLayout>
```
这个示例代码中,我们在MainActivity中实现了按钮1的点击事件处理和按钮2的点击事件处理,分别实现了弹出列表对话框和启动另一个界面,并将文本框内容传递到该界面中显示。在SecondActivity中,我们接收MainActivity传递过来的文本框内容,并在界面上显示出来。
阅读全文