package com.example.eg16_5;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		View.OnClickListener bt1lis = new View.OnClickListener(){
			public void onClick(View v) {
				showToast();
			}
		};
			
		View.OnClickListener bt2lis = new View.OnClickListener(){
		public void onClick(View v) {
				Toast.makeText(MainActivity.this,"直接输出测试", Toast.LENGTH_LONG).show();
			}
		};
		
		Button button1=(Button)findViewById(R.id.button1);
		button1.setOnClickListener(bt1lis);
		
	    Button button2=(Button)findViewById(R.id.button2);
	    button2.setOnClickListener(bt2lis);
	}

	public void showToast(){
		LayoutInflater li = (LayoutInflater)
				getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		View view = li.inflate(R.layout.toast,null);
		//把布局文件toast.xml转换成一个view
		Toast toast=new Toast(this);
		toast.setView(view);
		
		//载入view,即显示toast.xml的内容
		TextView tv=(TextView)view.findViewById(R.id.tv1);
		tv.setText("Toast显示View内容");
		//修改TextView里的内容
		toast.setDuration(Toast.LENGTH_SHORT);
		//设置显示时间，长时间Toast.LENGTH_LONG，
		//短时间为Toast.LENGTH_SHORT,不可以自己编辑;
		toast.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;
	}

}
