main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btn_long" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="长时间显示Toast"/> <Button android:id="@+id/btn_short" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="短时间显示Toast"/> </LinearLayout>
package org.lxh.demo; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.text.method.ScrollingMovementMethod; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.Button; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; public class Hello extends Activity { private Button btn_long = null; private Button btn_short = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 生命周期方法 super.setContentView(R.layout.main); // 设置要使用的布局管理器 this.btn_long = (Button) super.findViewById(R.id.btn_long); this.btn_short = (Button) super.findViewById(R.id.btn_short); this.btn_long.setOnClickListener(new OnClickListenerLong()); this.btn_short.setOnClickListener(new OnClickListenerShort()); } private class OnClickListenerLong implements OnClickListener { public void onClick(View view) { Toast.makeText(Hello.this, "长时间显示Toast提示信息框", Toast.LENGTH_LONG) .show(); } } private class OnClickListenerShort implements OnClickListener { public void onClick(View view) { Toast.makeText(Hello.this, "短时间显示Toast提示信息框", Toast.LENGTH_SHORT) .show(); } } }运行效果如下:
原文地址:http://blog.csdn.net/yayun0516/article/details/42918127