我的android项目中有一个main.xml,它有以下控件
TextBox1 Button1 TextBox2 Button2
TextBox3
TextBox4
label1的
将Button3
我有两个活动名称为DatePickerDialogAppsActivity和JSONSampleAppActivity。
Datepickerdialogappsactivity具有在button1和button2点击事件上弹出数据贴纸的功能
Jsonsampleactivity与我的sql数据库连接,并在button3点击事件上使用标签绑定数据,从textbox3和textbox4中的用户输入
问题:我的main.xml文件访问了datepickerdialogappsactivity和jsonsampleactivity,我应该如何定义活动和清单文件。
这是我的代码,每次只有一个活动运行意味着我在第一个节点上定义的每个活动它起作用,其次不起作用。例如,在下面的代码中,datepickerdialogappsactivity工作,如果我首先定义jsonsample活动,jsonsampleactivity不起作用,那么第二个将无法工作请指导
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".DatePickerDialogAppsActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".jsonsampleactivity"
android:label="@string/app_name" >
</activity>
</application>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="From Date"
>
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="To Date"
>
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="150px"
android:onClick="showDatePickerDialog"
android:text="From Date" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="150px"
android:onClick="showDatePickerDialogg"
android:text="To Date" />
<EditText
android:id="@+id/txtUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="User Name"
>
<requestFocus />
</EditText>
<EditText
android:id="@+id/txtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
/>
<Button
android:id="@+id/btnLogin"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/lblStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Status"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
package com.Android.JSONApp;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
public class DatePickerDialogAppsActivity extends Activity {
/** Called when the activity is first created. */
static final int DATE_DIALOG_ID=0;
static final int DATE_DIALOG_IDD=1;
int yr,mon,day, yrr, monn, dayy;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void showDatePickerDialog(View v)
{
Calendar today=Calendar.getInstance();
yr=today.get(Calendar.YEAR);
mon=today.get(Calendar.MONTH);
day=today.get(Calendar.DAY_OF_MONTH);
showDialog(DATE_DIALOG_ID);
}
public void showDatePickerDialogg(View v)
{
Calendar today=Calendar.getInstance();
yrr=today.get(Calendar.YEAR);
monn=today.get(Calendar.MONTH);
dayy=today.get(Calendar.DAY_OF_MONTH);
showDialog(DATE_DIALOG_IDD);
}
protected Dialog onCreateDialog(int id)
{
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,dateSetListener,yr,mon+1,day);
case DATE_DIALOG_IDD:
return new DatePickerDialog(this,dateSetListenerr,yrr,monn+1,dayy);
}
return null;
}
private DatePickerDialog.OnDateSetListener dateSetListener=new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
yr=year;
mon=monthOfYear +1;
day=dayOfMonth;
EditText it=(EditText)findViewById(R.id.editText1);
it.setText(day+" - "+mon+" - "+yr);
}
};
private DatePickerDialog.OnDateSetListener dateSetListenerr=new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int yearr, int monthOfYearr,
int dayOfMonthh) {
// TODO Auto-generated method stub
yrr=yearr;
monn=monthOfYearr +1;
dayy=dayOfMonthh;
EditText itt=(EditText)findViewById(R.id.editText2);
itt.setText(dayy+" - "+monn+" - "+yrr);
}
};
}
package com.Android.JSONApp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class JSONSampleAppActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
//Property declaration
Button btnLogin;
TextView lblStatus;
EditText txtUserName,txtPassword;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnLogin=(Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(this);
lblStatus=(TextView)findViewById(R.id.lblStatus);
txtUserName=(EditText)findViewById(R.id.txtUserName);
txtPassword=(EditText)findViewById(R.id.txtPassword);
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnLogin:
String userName=txtUserName.getText().toString();
String password=txtPassword.getText().toString();
if(verifyLogin(userName,password))
{
lblStatus.setText("Login Successful");
}
else
{
lblStatus.setText("Login Failed");
}
break;
}
}
public static String convertStreamToString(InputStream is)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static boolean verifyLogin(String UserName,String Password)
{
try
{
System.out.println("guru");
DefaultHttpClient httpClient=new DefaultHttpClient();
//Connect to the server
HttpGet httpGet=new HttpGet("http://xxx/Service1.svc/checkLogin?name="+UserName+"&pass="+Password);
//Get the response
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream stream=httpEntity.getContent();
//Convert the stream to readable format
String result= convertStreamToString(stream);
if(result.charAt(1)=='1')
{
return true;
}
else
{
return false;
}
}
catch(Exception e)
{
return false;
}
}
}
答案 0 :(得分:0)
如果两个Activities
共享相同的布局,则在功能方面确实不错(但至少在设计方面很奇怪,因为Activity
类旨在通过UI与用户进行交互):在Android中,只能同时显示一个Activity
。因此,您不能同时显示2个活动。如果您从另一个Activity
开始,则第一个将暂停。如果您希望在启动器中显示这两个活动,则它们都应具有以下内容:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
但其中一个也应注释为默认值:
<category android:name="android.intent.category.DEFAULT"/>
答案 1 :(得分:0)
在这两项活动中,请使用:
setContentView(R.layout.main);
但是,如果两个活动都有相同的布局,你想要实现什么?
根据经验,您应该始终为不同的活动使用不同的xml。
答案 2 :(得分:0)
您可以使用一个Activity来执行这两项操作,如下所示:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
static final int DATE_DIALOG_ID=0;
static final int DATE_DIALOG_IDD=1;
int yr,mon,day, yrr, monn, dayy;
Button btnLogin;
TextView lblStatus;
EditText txtUserName,txtPassword;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnLogin=(Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(this);
lblStatus=(TextView)findViewById(R.id.lblStatus);
txtUserName=(EditText)findViewById(R.id.txtUserName);
txtPassword=(EditText)findViewById(R.id.txtPassword);
}
public void showDatePickerDialog(View v)
{
Calendar today=Calendar.getInstance();
yr=today.get(Calendar.YEAR);
mon=today.get(Calendar.MONTH);
day=today.get(Calendar.DAY_OF_MONTH);
showDialog(DATE_DIALOG_ID);
}
public void showDatePickerDialogg(View v)
{
Calendar today=Calendar.getInstance();
yrr=today.get(Calendar.YEAR);
monn=today.get(Calendar.MONTH);
dayy=today.get(Calendar.DAY_OF_MONTH);
showDialog(DATE_DIALOG_IDD);
}
protected Dialog onCreateDialog(int id)
{
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,dateSetListener,yr,mon+1,day);
case DATE_DIALOG_IDD:
return new DatePickerDialog(this,dateSetListenerr,yrr,monn+1,dayy);
}
return null;
}
private DatePickerDialog.OnDateSetListener dateSetListener=new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
yr=year;
mon=monthOfYear +1;
day=dayOfMonth;
EditText it=(EditText)findViewById(R.id.editText1);
it.setText(day+" - "+mon+" - "+yr);
}
};
private DatePickerDialog.OnDateSetListener dateSetListenerr=new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int yearr, int monthOfYearr,
int dayOfMonthh) {
// TODO Auto-generated method stub
yrr=yearr;
monn=monthOfYearr +1;
dayy=dayOfMonthh;
EditText itt=(EditText)findViewById(R.id.editText2);
itt.setText(dayy+" - "+monn+" - "+yrr);
}
};
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnLogin:
String userName=txtUserName.getText().toString();
String password=txtPassword.getText().toString();
if(verifyLogin(userName,password))
{
lblStatus.setText("Login Successful");
}
else
{
lblStatus.setText("Login Failed");
}
break;
}
}
public static String convertStreamToString(InputStream is)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static boolean verifyLogin(String UserName,String Password)
{
try
{
System.out.println("guru");
DefaultHttpClient httpClient=new DefaultHttpClient();
//Connect to the server
HttpGet httpGet=new HttpGet("http://xxx/Service1.svc/checkLogin?name="+UserName+"&pass="+Password);
//Get the response
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream stream=httpEntity.getContent();
//Convert the stream to readable format
String result= convertStreamToString(stream);
if(result.charAt(1)=='1')
{
return true;
}
else
{
return false;
}
}
catch(Exception e)
{
return false;
}
}
}