public class MainActivity extends ActionBarActivity <span style="color:#FF0000;">implements OnClickListener</span> {
private EditText ed_num;
private EditText ed_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
<span style="color:#FF0000;">Button bt_send = (Button) findViewById(R.id.bt_send_message);</span>
ed_num = (EditText) findViewById(R.id.ed_num);
ed_content = (EditText) findViewById(R.id.ed_content);
<span style="color:#FF0000;">bt_send.setOnClickListener(this);</span>
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
<span style="color:#FF0000;">@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_send_message:
sendMes();
break;
}
}</span>
private void sendMes() {
String num = ed_num.getText().toString().trim();
String content = ed_content.getText().toString().trim();
if (TextUtils.isEmpty(num) || TextUtils.isEmpty(content)) {
Toast.makeText(MainActivity.this, "收件人或内容不能为空!!",
Toast.LENGTH_SHORT).show();
return;
} else {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(num, null, content, null, null);
}
}
}
android button 点击事件(最常使用)
最新推荐文章于 2023-03-11 11:37:20 发布