Android开发:检查EditText的内容是否为空白。如果空白,则在TextView里面显示:“请输入你的姓名!” 点击“确定”按钮后将EditText中的内容显示在TextView中。
时间: 2024-05-16 14:14:33 浏览: 5
好的,针对您的问题,我可以给出以下代码示例:
XML布局文件:
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入你的姓名" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextName"
android:layout_alignParentEnd="true"
android:layout_marginTop="8dp"
android:text="确定" />
<TextView
android:id="@+id/textViewName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btnSubmit"
android:text="您输入的姓名是:"
android:textSize="20sp" />
</RelativeLayout>
```
Java代码:
```java
public class MainActivity extends AppCompatActivity {
EditText editTextName;
Button btnSubmit;
TextView textViewName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextName = findViewById(R.id.editTextName);
btnSubmit = findViewById(R.id.btnSubmit);
textViewName = findViewById(R.id.textViewName);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = editTextName.getText().toString().trim();
if (TextUtils.isEmpty(name)) {
textViewName.setText("请输入你的姓名!");
} else {
textViewName.setText("您输入的姓名是:" + name);
}
}
});
}
}
```
以上代码实现了检查EditText的内容是否为空白,如果为空白则在TextView中显示“请输入你的姓名!”提示信息,如果不为空则在TextView中显示输入的姓名。希望能够帮到您。