標題:
土地面積換算 (二)
[打印本頁]
作者:
tonyh
時間:
2019-3-23 14:12
標題:
土地面積換算 (二)
本帖最後由 tonyh 於 2019-3-23 15:01 編輯
使用 OnClickListener 介面實作一傾聽者物件,藉由覆寫 onClick() 方法定義點擊後所要執行的動作,並以 setOnClickListener() 方法為元件加上傾聽者,完成如下之練習:
MainActivity.java
package com.example.student.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText editText;
TextView textView;
Button button1, button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.textView3);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText("");
textView.setText("");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double a = Double.parseDouble(editText.getText().toString());
double res = a * 3.3058;
//String res=String.format("%.4f",a*3.3058);
textView.setText("面積為: " + res + "平方公尺");
}
});
}
}
複製代碼
作者:
蕭澧邦
時間:
2019-3-23 15:10
本帖最後由 蕭澧邦 於 2019-3-27 13:01 編輯
package com.example.shou6.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button click;
private Button clean;
private EditText input;
private TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click = (Button) findViewById(R.id.button2);
clean = (Button) findViewById(R.id.button1);
input = (EditText) findViewById(R.id.editText);
result = (TextView) findViewById(R.id.textView3);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String res = String.format("%.4f",Double.parseDouble(input.getText().toString()) * 3.3058);
result.setText("面積為: " + res + "平方公尺");
}
});
clean.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
input.setText("");
result.setText("");
}
});
}
}
複製代碼
作者:
譚暐霖
時間:
2019-3-23 15:10
package com.example.student.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText editText;
TextView textView;
Button button, button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.textView3);
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText("");
textView.setText("");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double a = Double.parseDouble(editText.getText().toString());
double res = a * 3.3058;
textView.setText("面積為: " + res + "平方公尺");
}
});
}
}
複製代碼
作者:
高睿辰
時間:
2019-3-23 15:18
package com.example.student.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText editText;
TextView textView;
Button button1, button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.textView3);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText("");
textView.setText("");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double a = Double.parseDouble(editText.getText().toString());
double res = a * 3.3058;
//String res=String.format("%.4f",a*3.3058);
textView.setText("面積為: " + res + "平方公尺");
}
});
}
}
複製代碼
作者:
莊旻叡
時間:
2019-3-23 15:19
package com.example.student.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText editText;
TextView textView;
Button button, button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.textView3);
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText("");
textView.setText("");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double a = Double.parseDouble(editText.getText().toString());
double res = a * 3.3058;
textView.setText("面積為: " + res + "平方公尺");
}
});
}
}
複製代碼
作者:
蔡幸融
時間:
2019-3-26 19:08
package com.example.student.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText editText;
TextView textView;
Button button1, button2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.textView3);
button1 = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText("");
textView.setText("");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Double area=Double.parseDouble(editText.getText().toString());
textView.setText("面積為"+String.format("%.4f",area*3.3058)+"平方公尺");
}
});
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2