一.运行效果图
二.主要代码:
1.MainActivity.java
package cn.edu.bzu.helloandroid;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
{
private Button btnClick;
private EditText etName;
private TextView textView;
@Override
protected 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
{
@Override
public void onClick(View view)
{
String name=etName.getText().toString();
Toast toast=Toast.makeText(getApplicationContext(), "hello"+name,Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 400);
ImageView imageView= new ImageView(getApplicationContext());
imageView.setImageResource(R.drawable.smile);
LinearLayout toastView = (LinearLayout) toast.getView();
toastView.setOrientation(LinearLayout.HORIZONTAL);
toastView.addView(imageView, 0);
toast.show();
}
}
}
2.布局代码
<?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"
[html] view plain copy
android:background="@drawable/bg" <span style="font-family: Arial, Helvetica, sans-serif;"> ></span>
[html] view plain copy
<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="82dp"
android:layout_marginTop="74dp"
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="60dp"
android:layout_marginTop="40dp"
android:background="@drawable/handok"
/>
</RelativeLayout>
3.配置文件
<?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>
三.实验总结
本次实验的主要任务就是自定义一个toast,因为原生的toast不能满足要求。本次实验要实现的toast效果是:四角要是圆角,且toast里同时包含图片和文字,图片和文字并排显示,点击imageButton,显示“hello+输入的内容“,且toast显示时间较长。