Home > Mobile >  How to realize the view of the button and add a screen
How to realize the view of the button and add a screen

Time:09-16

The current I set a button control under LinearLayout layout and the View controls:
The android: id="@ + id/bt"
Android: layout_width="match_parent"
Android: layout_height="wrap_content"
Android: text="Button"
Android: visibility="visible"/& gt;

The android: id="@" + id/v
Android: layout_width="match_parent"
Android: layout_height="wrap_content"
Android: visibility="visible"/& gt;

I created a NewView class, however, for the View drawing circle:
Public class NewView extends the View {
Public NewView (Context Context, AttributeSet attrs) {
super(context, attrs);
}
Public NewView (MainActivity MainActivity) {
Super (mainActivity);
}
In the Mainactivity. Java write click a button control in the View control draw points:
Private Button Search;
Private View v.
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Initview ();
}
Public void initview () {
Search=the findViewById (R.i db t);
V=(View) the findViewById (R.i d.v);
Search. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View View) {
NewView NewView=new NewView (MainActivity. This);
The setContentView (newView);
}
});
}
But the test result is when I click on the button control, the button under the control of View controls not draw circle, but a button control in the phone's screen will disappear, new View controls to draw coordinate point, want to consult everybody bosses how to modify the code, can make it after the click on the button control, the View controls at the bottom of the button control shows I painted the coordinates of the point? Disappear after instead of clicking a button control, a button control, a new View controls map coordinate points, hope to lend a helping hand, help me, be badly in need of help,

CodePudding user response:

Public void initview () {
Search=the findViewById (R.i db t);
V=(View) the findViewById (R.i d.v);
Search. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View View) {
NewView NewView=new NewView (MainActivity. This);
The setContentView (newView);//this place can't use this statement, that is to change the content of the MainActivity
}
});
}
You this is to reset the MainActivity display content
I can feel the R.l ayout. Activity_main file, create two LinearLayout container, a container put a button, a container put a custom View, the listener to the button for a second in the LinearLayout with addView () to join you a new View of
Something like this
Public void onClick (View View) {
NewView NewView=new NewView (MainActivity. This);
The second LinearLayout ID. AddView (newView);//this place can't use this statement, that is to change the content of the MainActivity
}

CodePudding user response:

The answer of the second paragraph above code comments forget to delete the

CodePudding user response:

Big hello, thanks for answer, I change the other way, but encountered new problems, want to ask bosses privately, please bosses can give the contact way? Or add my QQ (3480618847) also, thank you thank you
  • Related