gpt4 book ai didi

java - 安卓开发: Combining two Java files

转载 作者:行者123 更新时间:2023-12-02 00:06:24 25 4
gpt4 key购买 nike

我有两个不同的 java 文件,想知道是否有一种方法可以将它们组合起来,以便两个代码都在一个文件中。

下面只是创建一个对话框,当单击“否”时,该对话框不执行任何操作;如果单击"is",则移动到新 Activity :

package com.example.top_tech_deals;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;



public class AlertDialogActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post);


Button btnAlertTwoBtns = (Button) findViewById(R.id.btnAlertWithTwoBtns);






/**
* Showing Alert Dialog with Two Buttons one Positive Button with Label
* "YES" one Negative Button with Label "NO"
*/
btnAlertTwoBtns.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
// Creating alert Dialog with two Buttons

AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);

// Setting Dialog Title
alertDialog.setTitle("Confirm Delete...");

// Setting Dialog Message
alertDialog.setMessage("Are you sure you want delete this?");

// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.delete);

// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick (DialogInterface dialog,int which) {
// Write your code here to execute after dialog

Intent k = new Intent(AlertDialogActivity.this, Camera.class);
startActivity(k);

Toast.makeText (getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick (DialogInterface dialog, int which) {
// Write your code here to execute after dialog
Toast.makeText (getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});

// Showing Alert Message
alertDialog.show();

}
});

}
}

第二个代码创建一个下拉菜单,其中包含用户可以选择的项目:

package com.example.top_tech_deals;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class AndroidSpinnerExampleActivity extends Activity implements OnItemSelectedListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinex);

// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner);

// Spinner click listener
spinner.setOnItemSelectedListener(this);

// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Automobile");
categories.add("Business Services");
categories.add("Computers");
categories.add("Education");
categories.add("Personal");
categories.add("Travel");

// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);

// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();

// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show ();

}

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

}

本质上,我想要的是我的 xml 文件之一同时使用对话框和下拉菜单,但是因为我无法将这两个代码放入一个 java 文件中,所以我不得不创建两个不同的 xml 文件,post.xml。链接到 AlertDialogActivity 的 xml 和链接到 AndroidSpinnerExampleActivity 的 spinex.xml。我尝试将两个java文件链接到同一个xml,但我发现只有一个java文件似乎做了任何事情,所以我相信唯一的解决方案是将这两个代码合并到一个java文件中,但我似乎总是遇到错误当我尝试这个时。

最佳答案

每个公共(public)类必须位于单独的文件中,文件名与类相同。但是,如果您将其中一个类更改为不公开,那么就可以了。我认为最好的做法是保持文件原样。将类放在单独的文件中对于将来的可维护性会更好,以便您可以轻松找到类。

关于java - 安卓开发: Combining two Java files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13779118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com