Android, XML view file and backend in Java, how can i create my custom view with backend java?

江枫思渺然 提交于 2019-12-12 02:08:40

问题


As we have main.xml file for view and another file java in src folder which contains the events, i have made another view, that onclicking a button in main.xml it will open the other view xml which should have a java backend file, when i create my custom view xml, then its backened file doesnot create, and how can i click a button and open a new view with another form, just like we do in web pages and how can our custom view have a java backend file.

Thanks Atif


回答1:


XML FILES

  1. firstXML.xml
  2. secondXML.xml

FILES

firstActivity.java

public class firstActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firstXML);
    }
}

secondActivity.java

public class secondActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondXML);
    }
}

Now if you have Button in firstXML. You want this Button to launch secondActivity:

firstActivity.java

public class firstActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firstXML);
        final Button button = (Button) findViewById(R.id.button_id);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent myIntent = new Intent(firstActivity.this,secondActivity.class);
                startActivityForResult(myIntent, 0);
            }
        });
    }
}

Some extra notes: Now if you want firstActivity to send some info to secondActivity

Change

Intent myIntent = new Intent(firstActivity.this,secondActivity.class);
startActivityForResult(myIntent, 0);

to

Intent myIntent = new Intent(firstActivity.this,secondActivity.class);
Bundle muBundle = new Bundle(); //create a Bundle
myBundle.putString("username","Sherif");
myBundle.putInt("userid",1234);
// EXAMINE THE [Bundle Class][2]
myIntent.putExtras(myBundle); //PUT THE Bundle you created in the Intent
startActivityForResult(myIntent, 0);

To capture this data in the secondActivity

public class secondActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondXML);
        Bundle myBundle = this.getIntent().getExtras(); //You got the bundle
        //TO USE THE BUNDLE
        String A = myBundle.getString("username"); // A = "Sherif"
        int B = myBundle.getInt("userid"); // B = 1234
    }
}


来源:https://stackoverflow.com/questions/6896590/android-xml-view-file-and-backend-in-java-how-can-i-create-my-custom-view-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!