標題:
Android App - basic view components
[打印本頁]
作者:
admin
時間:
2012-1-29 13:00
標題:
Android App - basic view components
把一些基本元件的應用介紹一下, 包括
EditText, Radio button, Checked box 及 toggle button
[attach]1005[/attach]
Java內容
package sample.radiogroup;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import android.widget.ToggleButton;
public class RadiogroupActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
// 當在 EditText 中按回車鍵, 便顯示 Toast Perform action on key press
Toast.makeText(RadiogroupActivity.this, edittext.getText(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
final CheckBox mycheckbox = (CheckBox) findViewById(R.id.checkbox);
mycheckbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
if (mycheckbox.isChecked()) {
Toast.makeText(RadiogroupActivity.this, "Selected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(RadiogroupActivity.this, "Not selected", Toast.LENGTH_SHORT).show();
}
}
});
final ToggleButton togglebutton = (ToggleButton) findViewById(R.id.togglebutton);
togglebutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
if (togglebutton.isChecked()) {
Toast.makeText(RadiogroupActivity.this, togglebutton.getText(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(RadiogroupActivity.this,togglebutton.getText(), Toast.LENGTH_SHORT).show();
}
}
});
final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);
radio_red.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(RadiogroupActivity.this, radio_red.getText(), Toast.LENGTH_SHORT).show();
}
});
final RadioButton radio_blue = (RadioButton) findViewById(R.id.radio_blue);
radio_blue.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(RadiogroupActivity.this, radio_blue.getText(), Toast.LENGTH_SHORT).show();
}
});
} // onCreate
}
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" >
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<CheckBox android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check it out"
android:checked="false"/> //決定啟動時有沒有打勾
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red" />
<RadioButton android:id="@+id/radio_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue" />
</RadioGroup>
<ToggleButton android:id="@+id/togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Happy"
android:textOff="Cry" />
</LinearLayout>
歡迎光臨 How2Do (http://forum.how2do.com.hk/)
Powered by Discuz! 7.2