Toast对象的使用
新建一个继承Activity类的ToastActivity,并设置布局文件为:toast.xml。
Toast组件的使用比较简单直接贴代码
这里通过Button直接将EditText的内容用Toast显示。
<EditText android:id="@+id/toast_edit01" android:layout_width="fill_parent" android:layout_height="wrap_content" />
<Button android:id="@+id/toast_btn01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/show_toast" /> |
最后通过代码实现。
public class ToastActivity extends Activity { private Button btn = null; private EditText edit = null;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.toast); this.btn = (Button) super.findViewById(R.id.toast_btn01); this.edit = (EditText) super.findViewById(R.id.toast_edit01); this.btn.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) { Toast.makeText(ToastActivity.this, edit.getText().toString(), Toast.LENGTH_SHORT).show(); } });
} |