初识android——Hello world!

选课方向,让我结识了android这门课程,从简单的课程开始,我编写了一个简单的android程序——打招呼!

 首先,修改activity_main.xml:

<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"
    android:background="@drawable/bg" 
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="42dp"
        android:text="@string/hello_world" />

        
    <TextView
        android:id="@+id/showResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="31dp"
        android:text="请输入你的姓名:" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:ems="10" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp"
        android:text="确定" />

</RelativeLayout>

修改string.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="android_name">打招呼</string>
    <string name="app1_name">打招呼</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!(你好)</string>
	<string name="hello">欢迎进入!</string>
    
    
</resources>

然后,修改MainActivity.java:

	private TextView showResult;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		showResult=(TextView) findViewById(R.id.showResult);
		showResult.setText("欢迎进入!");
		
		clickButton = (Button) findViewById(R.id.button1);
		nameText = (EditText) this.findViewById(R.id.editText1);
		
		//绑定事件源和事件处理者 委托处理
			clickButton.setOnClickListener(new ClickHandler());
		
	}
	
	//事件处理类
		private class ClickHandler implements OnClickListener {

			@Override
			public void onClick(View v) {
				//界面输出
				Toast.makeText(MainActivity.this,
						"你好" + nameText.getText().toString(), Toast.LENGTH_LONG)
						.show();
			}

		}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

在虚拟机中显示的界面:

提示:

在做这个程序过程中,遇到了很多问题,也让我接触到了一个新的编程环境。一开始做忘记把背景图片传到drawable中,以至于未显示背景,其中也有跳转不成功的种种问题,让我在编程中谨慎行之。


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值