(一)在linearLayout下使用relativeLayout
(二)MenuInflater使用方式
(三)onPrepareOptionsMenu使用方式,主要用於點擊事件
相關資料來源
由於系統內容有些不要必要程式,因此以註記的方式取代
題目說明:
英呎 = 公尺 * 3.28
公尺 = 英呎 * 0.3048
package COM.TQC.GDD02; import java.text.DecimalFormat; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class GDD02 extends Activity { private boolean MenuChange = false; //protected String PREF_M = "pref_m"; //protected String PREF_FT = "pref_ft"; //protected String pref_m; //protected String pref_ft; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button01=(Button)findViewById(R.id.submit01); Button button02=(Button)findViewById(R.id.submit02); button01.setOnClickListener(MtoFT); button02.setOnClickListener(FTtoM); } private Button.OnClickListener MtoFT = new Button.OnClickListener () { @Override public void onClick(View v) { // TODO Auto-generated method stub DecimalFormat nf = new DecimalFormat("0.00"); EditText input_value = (EditText)findViewById(R.id.input_value); double input = Double.parseDouble(input_value.getText().toString()); double MFT=input*3.28; TextView result=(TextView)findViewById(R.id.result); result.setText(input+"公尺="+nf.format(MFT)+"英呎"); MenuChange = false; } }; private Button.OnClickListener FTtoM = new Button.OnClickListener () { @Override public void onClick(View v) { // TODO Auto-generated method stub DecimalFormat nf = new DecimalFormat("0.00"); EditText input_value = (EditText)findViewById(R.id.input_value); double input = Double.parseDouble(input_value.getText().toString()); double MFT=input*0.3048; TextView result=(TextView)findViewById(R.id.result); result.setText(input+"英呎="+nf.format(MFT)+"公尺"); MenuChange = false; } }; /*@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); return true; }*/ public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()){ case R.id.menu_reset: ResetValue(); //呼叫ResetValue() break; } return true; } private void ResetValue() //重置 { EditText input_value = (EditText)findViewById(R.id.input_value); TextView result = (TextView)findViewById(R.id.result); input_value.setText(""); result.setText(""); MenuChange=true; } public boolean onPrepareOptionsMenu(Menu menu) { if(MenuChange) menu.clear(); else { if(menu.size()<=0) { MenuInflater inflater = getMenuInflater();//解析定義menu目錄下的選單佈局文件 inflater.inflate(R.menu.menu, menu); } } return super.onPrepareOptionsMenu(menu); } }