I am adding a Button to my first Wear OS application. When I follow a model from my existing Android applications, there appears to be difference due to "WearableActivity" versus "Activity". I cannot define the OnClickListener.
In my on create is this:
bottomButton = findViewById(R.id.bottomButton);
setListener();
later in the main activity source is this
void setListener()
{
bottomButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) // toggle between report and graph display
{ if (showGraph)
{showReport=true; showGraph=false;bottomButton.setText("show Report");}
else
{showReport=false;showGraph=true;bottomButton.setText("show Graph");}
} // end, onClick
}); // end, setOnClickListener
} // end, setListener()
In both cases the button is defined in XML and found and used as follows
OkButton = (Button) findViewById(R.id.OkButton);
OkButton.setOnClickListener(this);
it does compile anyway. And I can access the items in the button such as text. But when I change the text, it doesn't appear on the screen. And when I push the button in the emulator, it doesn't register any action.
The problem is not from your code but from the Android wear emulator. Your wear emulator is in the ambient(low power) mode. Click in the top of the emulator window to toggle between interactive(full power) and ambient modes(low power).
How can I define and use a button in the wear os application.
The same way it is done on the mobile side. Below is an example
public class MainActivity extends WearableActivity implements View.OnClickListener {
Button clickMeButton;
TextView textView;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAmbientEnabled();
clickMeButton = findViewById(R.id.click_me_button);
textView = findViewById(R.id.test_textview);
clickMeButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
count++;
textView.setText("I am clicked: "+count);
}
@Override
public void onEnterAmbient(Bundle ambientDetails) {
// Handle entering ambient mode
super.onEnterAmbient(ambientDetails);
Log.e("Hello", "I'm ambient");
}
@Override
public void onExitAmbient() {
// Handle exiting ambient mode
super.onExitAmbient();
Log.e("Hello", "exit ambient");
}
@Override
public void onUpdateAmbient() {
// Update the content
super.onUpdateAmbient();
Log.e("Hello", "update ambient: " + isAmbient());
}
}
Below is the results of the above activity (NB: Click in the top of the emulator window to toggle between interactive and ambient modes):
EDIT
To avoid implementing View.OnClickListener in your activity you can pass an instance of View.OnClickListener when calling setOnClickListener
for the OkButton
like you are already doing for the bottomButton
.
OkButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something when OkButton is clicked
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.