Android第一个小程序

由于刚开学,也是刚开始学习Android,前几节课只是老师在课堂上讲讲,我呢,就坐在下面随便听听,可能是因为自己已经学习Java两年了(自认为比老师要高明,呵呵)学习Android开发不是太难,课下也就没怎么练习,直到今天下午第二节课上,老师让用前面所讲的知识做练习时,我左思右想,这样写那样敲,总是做不出来,这才意识到自己太自以为是了,确实应该静下心来好好梳理梳理知识了,不能再如此的浮躁啦!现将张老师所出之题目,和我自己做的答案摘写如下:

题目:

实验要求

(结果图和要求都显示在图片上)

代码如下:

MainActivity.java

package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

 private EditText username,password;
 private Button bt1,bt2;
 private TextView tv1,tv2;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  username = (EditText)super.findViewById(R.id.username);
  password = (EditText)super.findViewById(R.id.password);
  bt1 = (Button)super.findViewById(R.id.bt1);
  bt2 = (Button)super.findViewById(R.id.bt2);
  tv1 = (TextView)super.findViewById(R.id.tv1);
  tv2 = (TextView)super.findViewById(R.id.tv2);
  
  bt1.setOnClickListener(new ShowListener1());
  bt2.setOnClickListener(new ShowListener2());
  
 }

 @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;
 }
 
 private class ShowListener1 implements OnClickListener
 {

  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   String username_info = username.getText().toString();
   tv1.setText(username_info);
   
  }
  
 }
 private class ShowListener2 implements OnClickListener
 {

  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   String password_info = password.getText().toString();
   tv2.setText(password_info);
   
  }
  
 }
}

 activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    
    
    >
    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <ImageView 
            android:id="@+id/img"
            android:src="@drawable/logo"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
         />
        <EditText 
            android:id="@+id/username"
         android:layout_marginTop="10dp"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:textSize="20sp"
         android:textColor="#AB1213"
         android:text="请输入用户名"
        />
    </LinearLayout>

        <EditText 
         android:id="@+id/password"
         android:password="true"
         android:textColor="#63CF08"
         android:textSize="20sp"
         android:paddingTop="10dp"
         android:paddingLeft="10dp"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:hint="请输入密码"
        />
        <Button 
            android:id="@+id/bt1"
            android:text="登录"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:width="100px"
            />
       <Button 
            android:id="@+id/bt2"
            android:text="验证密码"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:width="100px"
            />
       <TextView 
           android:id="@+id/tv1"
           android:textSize="20sp"
           android:textColor="#09488F"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           />
       <TextView 
           android:id="@+id/tv2"
           android:textSize="20sp"
           android:textColor="#221F25"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           />    
    
</LinearLayout>


以上代码经过测试运行结果为:

实验结果图片

总结:不管怎么样,这也是花了我还几个小时的成果,总算不管好坏基本符合要求。这只是一个最简单的Android程序,由于我的懒惰和骄傲却花了我这么长时间,真是得不偿失,以后不能再这样了,尽管这是这是专业限选课,但既然选了,既然对它有兴趣,就应该好好对待,毕竟多学一门技术毕业后找工作时多一个筹码。

以后我会认真学习这门课认真总结,并整理成博客,激励自己,或许也能对其他对Android开发有兴趣的朋友些许帮助。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值