<pre style="font-family: Consolas; font-size: 11.3pt; background-color: rgb(255, 255, 255);"><span style="color:#000080;"><strong>《MainActivity.java》</strong></span>
package com.example.huang.submit0;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast; //等价于C++的输入即cin
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private EditText user,pw; //定义私有的EditText和Button
private Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user=(EditText)findViewById(R.id.username); //使用findViewById方法来获取资源中的ID组件
pw=(EditText)findViewById(R.id.password);
submit=(Button)findViewById(R.id.button);
submit.setOnClickListener(new View.OnClickListener() { //监听即连接AppCompatActivity
@Override
public void onClick(View v) {
String username=user.getText().toString();
String password=pw.getText().toString();
// Toast.makeText(MainActivity.this, ""+username+""+password, Toast.LENGTH_SHORT).show();
setTitle(""+username+" 密码:"+password); //替代文件名在 标题上显示
}
});
}
}
《activity_main.xml》 可视化操作=手动添加(Text中) || 拖动控件(Design.Palette中)
<?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"//设置布局占屏幕的高度设置,设置高度全屏
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.huang.submit0.MainActivity">
<EditText
android:layout_width="wrap_content" //布局宽度,代表根据内容自动拉伸,使内容全部呈现;
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="用户名:" //给控件命名
android:ems="10"
android:id="@+id/username" //设置控件id
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:id="@+id/button"
android:layout_centerVertical="true" //将控件置于父控件(布局容器)垂直居中位置
android:layout_alignParentLeft="true" //位于父容器左上角
//只能在父控件为RelativeLayout时才起作用,而对于像LinearLayout这样的布局不起作用
android:layout_alignParentStart="true" />
</RelativeLayout>
注:<RelativeLayout>成对存在</RelativeLayout>
<Design>可双击父对话框内的控件即弹出小框可编辑空间的text和id;
</pre>