所以我对android很新,我正在研究android网站上的一些示例代码。我现在正在看的那个叫做通知用户:http://developer.android.com/training/notify-user/index.html。
示例非常简单,用户以秒为单位输入时间和消息,并在后台设置计时器。一旦时间用完,就会通知用户。
所以这是activity_main.xml的副本
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/seconds_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/ping_text" />
<EditText
android:id="@+id/edit_seconds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/seconds_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:ems="10"
android:text="@string/seconds_default"
android:inputType="numberSigned">
<requestFocus />
</EditText>
<TextView
android:id="@+id/reminder_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edit_seconds"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/reminder_label" />
<EditText
android:id="@+id/edit_reminder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/edit_seconds"
android:layout_below="@+id/reminder_view"
android:layout_marginTop="15dp"
android:ems="10"
android:text="@string/reminder_text"
android:inputType="textMultiLine" />
<Button
android:id="@+id/ping_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edit_reminder"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:onClick="onPingClick"
android:text="@string/ping" />
</RelativeLayout>
您可以看到有两个编辑视图供用户输入秒(edit_seconds)和消息(edit_reminder)。还有一个名为ping的按钮,当按下时将数据发送到ServiceIntent。
以下是mainactivity.java的代码:
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.example.android.pingme;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private Intent mServiceIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creates an explicit Intent to start the service that constructs and
// issues the notification.
mServiceIntent = new Intent(getApplicationContext(), PingService.class);
}
/*
* Gets the values the user entered and adds them to the intent that will be
* used to launch the IntentService that runs the timer and issues the
* notification.
*/
public void onPingClick(View v) {
int seconds;
// Gets the reminder text the user entered.
EditText msgText = (EditText) findViewById(R.id.edit_reminder);
String message = msgText.getText().toString();
mServiceIntent.putExtra(CommonConstants.EXTRA_MESSAGE, message);
mServiceIntent.setAction(CommonConstants.ACTION_PING);
Toast.makeText(this, R.string.timer_start, Toast.LENGTH_SHORT).show();
// The number of seconds the timer should run.
EditText editText = (EditText)findViewById(R.id.edit_seconds);
String input = editText.getText().toString();
if(input == null || input.trim().equals("")){
// If user didn't enter a value, sets to default.
seconds = R.string.seconds_default;
} else {
seconds = Integer.parseInt(input);
}
int milliseconds = (seconds * 1000);
mServiceIntent.putExtra(CommonConstants.EXTRA_TIMER, milliseconds);
// Launches IntentService "PingService" to set timer.
startService(mServiceIntent);
从上面的onPingClick(View v)方法可以看出,editTexts都引用了变量“msgText”和“editText”。然而,ping按钮从未被引用,也没有附加到按钮的监听器,因此它在按下按钮时启动服务。但是,该应用程序可以在模拟器和手机上完美运行,而无需引用按钮。
android如何知道单击按钮意味着必须对onPingClick(View v)的方法进行操作?
答案 0 :(得分:1)
因为在指定Button的xml布局中:android:onClick="onPingClick"
这会在Java代码之外设置onClickListener。
请注意,方法符合签名:
public void someName(View v)
这是为视图设置Click Listener的另一种方法。