我有两个不同的java文件,并且想知道是否有办法将它们组合起来,以便两个代码都在一个文件中。
下面简单地创建一个对话框,在没有单击时不执行任何操作,如果单击是,则移动到新活动:
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文件。链接到AlertDialogActivity的xml和链接到AndroidSpinnerExampleActivity的spinex.xml。我尝试将两个java文件链接到同一个xml但我发现只有一个java文件似乎做了什么,所以我相信唯一的解决办法就是将这两个代码组合成一个java文件,但我似乎总是会出错我试试的时候。
答案 0 :(得分:1)
每个公共类必须位于单独的文件中,文件名与类相同。但是,如果您将其中一个课程更改为不公开,那么确定。我认为最佳做法是保持文件不变。将您的课程放在单独的文件中,以便将来可维护性更好,这样您就可以轻松找到您的课程。
答案 1 :(得分:1)
基本上我想要的是我的一个xml文件同时使用一个对话框 框和下拉菜单,但是因为我在放置时遇到了麻烦 将这两个代码放入一个java文件中,我使用了两个 不同的xml文件,post.xml链接到AlertDialogActivity 和spinex.xml链接到AndroidSpinnerExampleActivity。
请按照以下步骤操作:
Button
和Spinner
。Activity
并实现OnItemSelectedListener
接口onCreate
方法中的:
setContentView(R.layout.combinedlayout);
// must be in the combinedlayout
Button btnAlertTwoBtns = (Button) findViewById(R.id.btnAlertWithTwoBtns);
btnAlertTwoBtns.setOnClickListener(new View.OnClickListener() {
// the rest of the listener code from your first class.
}
// must be in the combinedlayout
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);
答案 2 :(得分:1)
我对你的问题有点困惑。你想组合这些活动,以便你有一个活动,同时打开对话框的按钮和屏幕上的微调器?如果是这样,你应该能够做到这一点。
只需创建一个包含按钮和微调器的xml文件:
<Button
.... />
<Spinner
.... />
然后,你可以只有一个Activity来引用微调器和按钮,填充微调器并将一个onItemSelectedListener赋给微调器。您可以像以前一样处理按钮。
import android.widget.AdapterView.OnItemSelectedListener;
public class AndroidSpinnerAndButtonExampleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// 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);
setSpinnerOnItemSelectedListener(spinner);
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(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(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();
}
});
}
public void setSpinnerOnItemSelectedListener(Spinner spinner){
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@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
}
});
}
}