我有兩個不同的java文件,並想知道是否有一種方法來組合它們,以便兩個代碼都在一個文件中。下面簡單Android開發:結合兩個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文件,鏈接到的post.xml AlertDialogActivity和鏈接到AndroidSpinnerExampleActivity的spinex.xml。我試圖將兩個java文件鏈接到同一個xml,但我發現只有一個java文件似乎做了任何事情,所以我相信唯一的解決方案是將兩個代碼合併到一個java文件中,但我似乎總是出錯當我嘗試這個。
結合Java文件的目標是什麼? –
以防萬一,我不清楚,第一個代碼創建一個對話框第二部分創建一個微調(下拉列表),這兩者都是在兩個不同的Java文件。目前我有兩個不同的xml文件,但我想將這兩個java代碼合併爲一個,這樣我就可以使用對話框和微調器來創建一個xml文件。 – JimmyK
你可以做到這一點,但對於你必須刪除公衆從已創建的Java類的一個又一個修改將是公共類,這將是我曾嘗試刪除的一個公共部分Java文件名 –