package hongye.com.hongyeapplicationblis;
import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
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 View.OnClickListener {
Button common,picture,text$picture,textThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
common = (Button) findViewById(R.id.bt_common_toast);
picture = (Button) findViewById(R.id.bt_picture_toast);
text$picture = (Button) findViewById(R.id.bt_parent_text_toast);
textThread = (Button) findViewById(R.id.bt_parent_Thread_toast);
common.setOnClickListener(this);
picture.setOnClickListener(this);
text$picture.setOnClickListener(this);
textThread.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_common_toast:
showCommonToas("这是一个普通的Toas");
break;
case R.id.bt_picture_toast:
showPicture();
break;
case R.id.bt_parent_text_toast:
showPictureText();
break;
case R.id.bt_parent_Thread_toast:
showToasThread();
break;
}
}
/**
* 普通的toas
*
* @param str
*/
private void showCommonToas(String str) {
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
/**
* 图片toas
*/
private void showPicture() {
Log.i("TAG","======图片toas=======");
Toast toast = new Toast(MainActivity.this);
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.mipmap.bears);
toast.setView(imageView);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
}
/**
* 图文并存
*/
private void showPictureText(){
Toast toast = new Toast(MainActivity.this);
ImageView iv = new ImageView(MainActivity.this);
iv.setImageResource(R.mipmap.bears);
TextView textView = new TextView(MainActivity.this);
textView.setText("我是Toast中的一只小熊");
textView.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout mLinearLayout = new LinearLayout(MainActivity.this);
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
mLinearLayout.addView(iv);
mLinearLayout.addView(textView);
toast.setView(mLinearLayout);
toast.show();
}
/**
* 线程条用Toas
*/
private void showToasThread(){
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
Toast.makeText(MainActivity.this, "我来自子线程线程!",Toast.LENGTH_SHORT).show();
Looper.loop();
}
}).start();
}
}Android Toast的个性使用