给我写一个代码,要安卓的程序
时间: 2024-02-22 14:59:42 浏览: 19
好的,以下是一个简单的安卓程序的代码示例,包含一个按钮和一个文本框,点击按钮后会在文本框中显示一条信息。
```
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText inputText;
private Button submitButton;
private TextView outputText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputText = (EditText) findViewById(R.id.input_text);
submitButton = (Button) findViewById(R.id.submit_button);
outputText = (TextView) findViewById(R.id.output_text);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = inputText.getText().toString();
outputText.setText("您输入的内容是:" + text);
}
});
}
}
```
这个程序的布局文件 `activity_main.xml` 包含一个 EditText、一个 Button 和一个 TextView。
```
<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=".MainActivity">
<EditText
android:id="@+id/input_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入内容"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:layout_below="@+id/input_text"
android:layout_alignParentRight="true"
android:layout_marginTop="20dp"/>
<TextView
android:id="@+id/output_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/submit_button"
android:layout_marginTop="20dp"/>
</RelativeLayout>
```
希望这个代码示例能够帮到你!
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)