任务1---打招呼
来源:互联网 发布:苦海翻起爱恨 知乎 编辑:程序博客网 时间:2024/06/06 07:52
一、运行效果
二、核心代码
1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" > <EditText android:id="@+id/etName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="45dp" android:layout_marginTop="94dp" android:background="@android:drawable/edit_text" android:drawableLeft="@drawable/name" android:ems="10" android:hint="@string/etNamePrompt" > <requestFocus /> </EditText> <Button android:id="@+id/btnClick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/etName" android:layout_below="@+id/etName" android:layout_marginLeft="70dp" android:layout_marginTop="30dp" android:background="@drawable/btn_bg" android:onClick="hello" /></RelativeLayout>
2.HelloAndroid Mainfest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.edu.bzu.helloandroid" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="cn.edu.bzu.helloandroid.MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
3.MainActivity.java
package cn.edu.bzu.helloandroid;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.Toast;public class MainActivity extends Activity {private Button btnClick;private EditText etName;private Toast toast;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnClick = (Button) findViewById(R.id.btnClick);btnClick.setOnClickListener(new ClickHandler());etName = (EditText) findViewById(R.id.etName);}class ClickHandler implements OnClickListener {@Overridepublic void onClick(View view) {String name = etName.getText().toString();toast = Toast.makeText(MainActivity.this, "Hello " + name + " !",Toast.LENGTH_LONG);/*toast.setGravity(Gravity.CENTER, 0, 0);RelativeLayout toastView = (RelativeLayout) toast.getView();ImageView imageCodeProject = new ImageView(getApplicationContext());imageCodeProject.setImageResource(R.drawable.smile);toastView.addView(imageCodeProject, 0);*/toast.show();}}}
三、程序出现的问题
1.添加带图片的toast时,加入代码
toast.setGravity(Gravity.CENTER, 0, 0);
RelativeLayout toastView = (RelativeLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.smile);
toastView.addView(imageCodeProject, 0);
运行时出现错误,程序停止运行,出现主函数致命异常,不加这几行代码程序可正常运行。问题还没有解决,希望老师指点。
2.对于调用方法以及方法中传入参数不能运用的熟练,对基本语法掌握不熟练,运用得先去查资料,写个小程序需要很长时间。
0 0
- 实验任务1 打招呼
- 任务1---打招呼
- 任务1---打招呼
- 实验二任务1打招呼
- 实验二 任务1 打招呼
- 任务一,打招呼。
- 实验二.任务一.打招呼
- 安卓任务二打招呼
- 实验二--任务一--打招呼
- Android案例1 打招呼
- 打招呼
- 打招呼
- 打招呼
- 打招呼
- 打招呼
- 打招呼
- 打招呼
- 打招呼
- 插入排序之直接插入排序(Java篇)
- HDU5119 动态规划入门题
- c++数组地址解析
- 【室内园艺】——栀子花
- 入门训练 Fibonacci数列
- 任务1---打招呼
- 2016年GitHub上史上最全的Android开源项目分类汇总
- Unity利用Xml和Json保存场景
- BGP边界路由协议笔记
- Vue.js——vue-resource全攻略【5】
- Hibernate映射解析——七种映射关系
- android studio git 配置及使用
- Linux笔记--gcc与gdb总结
- 内存泄露和野指针的概念