close

Add a new package

The new package's name is mylistview

Add an activity for the new package

The new Java class's name is MyListViewActivity

The snippet code looks like this

package com.example.listview.mylistview;

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.Nullable;

public class MyListViewActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.);

}

And ready to add a new layout for a minute

The file name is activity_mylistview and its root is LinearLayout

---------------

An empty layout for activity_mylistview.xml is created successfully

And its XML stuff is shown below

  <?
  xml version
  ="1.0" 
  encoding
  ="utf-8"
  ?>
<LinearLayout xmlns: android ="http://schemas.android.com/apk/res/android"
android :orientation ="vertical"
android :layout_width ="match_parent"
android :layout_height ="match_parent" >

</LinearLayout>

so we can now go back to MyListViewActivity.java

1. import com.example.listview.R

2. setContentView

Modify manifests->AndroidManifest.xml

----------------------------------------

mylistview is the package's name that I created at the beginning.

In MainActivity.java, we add a button called m_btn, which is initialized by using findViewById method

And set its onClickListener in order to go to another page.

public class MainActivity extends AppCompatActivity {

Button m_btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_btn = findViewById(R.id.btn_listview);
m_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MyListViewActivity.class);
startActivity(intent);
}
});
}
}

---------------------------------------------------------

 

 

 

 

arrow
arrow
    全站熱搜

    me1237guy 發表在 痞客邦 留言(0) 人氣()