package com.example.toast;
import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private Button but1;
private Button but2;
private Button but3;
private Button but4;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(this);
Button b2=(Button)findViewById(R.id.button2);
b2.setOnClickListener(this);
Button b3=(Button)findViewById(R.id.button3);
b3.setOnClickListener(this);
Button b4=(Button)findViewById(R.id.button4);
b4.setOnClickListener(this);
}
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;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button1:
toast1();
break;
case R.id.button2:
toast2();
break;
case R.id.button3:
toast3();
break;
case R.id.button4:
toast4();
break;
}
}
private void toast4() {
// TODO Auto-generated method stub
Toast t=new Toast(this);
t.setGravity(Gravity.CENTER,0,0);
ImageView iv=new ImageView(this);
iv.setImageResource(R.drawable.ic_launcher);
TextView tv=new TextView(this);
tv.setText("图片信息");
LinearLayout view=new LinearLayout(this);
view.setOrientation(LinearLayout.HORIZONTAL);
view.addView(iv);
view.addView(tv);
t.setView(view);
t.show();
}
//图文提示信息
private void toast3() {
//构建Toast对象
Toast t=Toast.makeText(this, "图片属性", Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER, 0, 0);//设置显示位置
//获取图片
ImageView iv=new ImageView(this);
iv.setImageResource(R.drawable.ic_launcher);
//由Toast对象获取Layout对象;同学A向老师申请要和同学B一起去表演
LinearLayout view =(LinearLayout)t.getView();//向老师申请
view.setOrientation(LinearLayout.HORIZONTAL);//设置该组件布局方式
//老师将B同学一同去表演 插入图片组件
view.addView(iv,0);
t.show();
}
//图片提示信息
private void toast2() {
// TODO Auto-generated method stub
Toast t=new Toast(this);
t.setDuration(Toast.LENGTH_LONG);//设置时间
ImageView iv=new ImageView(this);
iv.setImageResource(R.drawable.ic_launcher);
//将某个组件放到指定组件显示。在指定组件类设置
t.setView(iv);
//设置位置
t.setGravity(Gravity.CENTER|Gravity.TOP,0,0);
t.show();
}
//文本提示信息
private void toast1() {
//
Toast t=Toast.makeText(this,"文本提示",Toast.LENGTH_LONG);
t.show();
}
}
import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private Button but1;
private Button but2;
private Button but3;
private Button but4;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(this);
Button b2=(Button)findViewById(R.id.button2);
b2.setOnClickListener(this);
Button b3=(Button)findViewById(R.id.button3);
b3.setOnClickListener(this);
Button b4=(Button)findViewById(R.id.button4);
b4.setOnClickListener(this);
}
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;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button1:
toast1();
break;
case R.id.button2:
toast2();
break;
case R.id.button3:
toast3();
break;
case R.id.button4:
toast4();
break;
}
}
private void toast4() {
// TODO Auto-generated method stub
Toast t=new Toast(this);
t.setGravity(Gravity.CENTER,0,0);
ImageView iv=new ImageView(this);
iv.setImageResource(R.drawable.ic_launcher);
TextView tv=new TextView(this);
tv.setText("图片信息");
LinearLayout view=new LinearLayout(this);
view.setOrientation(LinearLayout.HORIZONTAL);
view.addView(iv);
view.addView(tv);
t.setView(view);
t.show();
}
//图文提示信息
private void toast3() {
//构建Toast对象
Toast t=Toast.makeText(this, "图片属性", Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER, 0, 0);//设置显示位置
//获取图片
ImageView iv=new ImageView(this);
iv.setImageResource(R.drawable.ic_launcher);
//由Toast对象获取Layout对象;同学A向老师申请要和同学B一起去表演
LinearLayout view =(LinearLayout)t.getView();//向老师申请
view.setOrientation(LinearLayout.HORIZONTAL);//设置该组件布局方式
//老师将B同学一同去表演 插入图片组件
view.addView(iv,0);
t.show();
}
//图片提示信息
private void toast2() {
// TODO Auto-generated method stub
Toast t=new Toast(this);
t.setDuration(Toast.LENGTH_LONG);//设置时间
ImageView iv=new ImageView(this);
iv.setImageResource(R.drawable.ic_launcher);
//将某个组件放到指定组件显示。在指定组件类设置
t.setView(iv);
//设置位置
t.setGravity(Gravity.CENTER|Gravity.TOP,0,0);
t.show();
}
//文本提示信息
private void toast1() {
//
Toast t=Toast.makeText(this,"文本提示",Toast.LENGTH_LONG);
t.show();
}
}
本文介绍了一款Android应用中如何通过Toast实现图文提示信息与文本提示信息的功能,包括创建Toast对象、设置显示位置、添加图片与文本等关键步骤。
724

被折叠的 条评论
为什么被折叠?



