所以我创建了一个navigationDrawer,我遇到了问题
我需要进行活动
ActivitySec
package com.calculatorvamal.app;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class SecondAct2 extends Activity implements TextWatcher {
private EditText Result;
private String Operation;
private EditText editYear;
private EditText editCapacity;
private Button calc;
private RadioButton radioD;
private RadioButton radioB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_home2);
TextView Year = (TextView) findViewById(R.id.Year);
TextView Capacity = (TextView) findViewById(R.id.Capacity);
editYear = (EditText) findViewById(R.id.editYear);
editYear.addTextChangedListener(this);
editCapacity = (EditText) findViewById(R.id.editCapacity);
final EditText Result = (EditText) findViewById(R.id.Result);
final Button calc = (Button) findViewById(R.id.calc);
final RadioButton radioD = (RadioButton) findViewById(R.id.radioD);
final RadioButton radioB = (RadioButton) findViewById(R.id.radioB);
TextWatcher listener = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (editYear.getText().toString().length() == 0 || editCapacity.getText().toString().length() == 0) {
calc.setEnabled(false);
} else {
calc.setEnabled(true);
}
}
};
editYear.addTextChangedListener(listener);
editCapacity.addTextChangedListener(listener);
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int edityear_value = Integer.parseInt(editYear.getText().toString());//Convert to intger
double result = 0;
int NumberCapacity = Integer.parseInt(editCapacity.getText().toString());//Convert to intger
if (radioB.isChecked()) {
switch (edityear_value) {
case 2004:
Operation = "2004";
break;
case 2005:
Operation = "2005";
break;
case 2006:
Operation = "2006";
break;
case 2007:
Operation = "2007";
break;
case 2008:
Operation = "2007";
break;
case 2009:
Operation = "2009";
break;
case 2010:
Operation = "2009";
break;
case 2011:
Operation = "2011";
break;
case 2012:
Operation = "2011";
break;
case 2013:
Operation = "2013";
break;
case 2014:
Operation = "2013";
break;
default:
Operation = "error";
}
} else if (radioD.isChecked()) {
switch (edityear_value) {
case 2004:
Operation = "20041";
break;
case 2005:
Operation = "20051";
break;
case 2006:
Operation = "20061";
break;
case 2007:
Operation = "20071";
break;
case 2008:
Operation = "20071";
break;
case 2009:
Operation = "20091";
break;
case 2010:
Operation = "20091";
break;
case 2011:
Operation = "20111";
break;
case 2012:
Operation = "20111";
break;
case 2013:
Operation = "20131";
break;
case 2014:
Operation = "20131";
break;
default:
Operation = "error";
}
}
//Benzin
if (NumberCapacity > 0 && NumberCapacity <= 1000) {
if (Operation.equals("2004")) {
result = NumberCapacity * 0.52;
}
if (Operation.equals("2005")) {
result = NumberCapacity * 0.50;
}
if (Operation.equals("2006")) {
result = NumberCapacity * 0.47;
}
if (Operation.equals("2007")) {
result = NumberCapacity * 0.44;
}
if (Operation.equals("2009")) {
result = NumberCapacity * 0.43;
}
if (Operation.equals("2009")) {
result = NumberCapacity * 0.43;
}
if (Operation.equals("2011")) {
result = NumberCapacity * 0.42;
}
if (Operation.equals("2013")) {
result = NumberCapacity * 0.40;
}
}
if (NumberCapacity > 1001 && NumberCapacity <= 1500) {
if (Operation.equals("2004")) {
result = NumberCapacity * 0.67;
}
if (Operation.equals("2005")) {
result = NumberCapacity * 0.63;
}
if (Operation.equals("2006")) {
result = NumberCapacity * 0.60;
}
if (Operation.equals("2007")) {
result = NumberCapacity * 0.56;
}
if (Operation.equals("2009")) {
result = NumberCapacity * 0.55;
}
if (Operation.equals("2011")) {
result = NumberCapacity * 0.54;
}
if (Operation.equals("2013")) {
result = NumberCapacity * 0.52;
}
}
if (NumberCapacity >= 1501 && NumberCapacity <= 2000) {
if (Operation.equals("2004")) {
result = NumberCapacity * 1.00;
}
if (Operation.equals("2005")) {
result = NumberCapacity * 0.95;
}
if (Operation.equals("2006")) {
result = NumberCapacity * 0.89;
}
if (Operation.equals("2007")) {
result = NumberCapacity * 0.84;
}
if (Operation.equals("2009")) {
result = NumberCapacity * 0.83;
}
if (Operation.equals("2011")) {
result = NumberCapacity * 0.82;
}
if (Operation.equals("2013")) {
result = NumberCapacity * 0.80;
}
}
if (NumberCapacity >= 2001 && NumberCapacity <= 3000) {
if (Operation.equals("2004")) {
result = NumberCapacity * 1.59;
}
if (Operation.equals("2005")) {
result = NumberCapacity * 1.52;
}
if (Operation.equals("2006")) {
result = NumberCapacity * 1.44;
}
if (Operation.equals("2007")) {
result = NumberCapacity * 1.36;
}
if (Operation.equals("2009")) {
result = NumberCapacity * 1.35;
}
if (Operation.equals("2011")) {
result = NumberCapacity * 1.34;
}
if (Operation.equals("2013")) {
result = NumberCapacity * 1.32;
}
}
if (NumberCapacity >= 3000) {
if (Operation.equals("2004")) {
result = NumberCapacity * 3.57;
}
if (Operation.equals("2005")) {
result = NumberCapacity * 3.56;
}
if (Operation.equals("2006")) {
result = NumberCapacity * 3.55;
}
if (Operation.equals("2007")) {
result = NumberCapacity * 3.54;
}
if (Operation.equals("2009")) {
result = NumberCapacity * 3.53;
}
if (Operation.equals("2011")) {
result = NumberCapacity * 3.52;
}
if (Operation.equals("2013")) {
result = NumberCapacity * 3.50;
}
}
//Diesel
if (NumberCapacity <= 1500) {
if (Operation.equals("20041")) {
result = NumberCapacity * 0.67;
}
if (Operation.equals("20051")) {
result = NumberCapacity * 0.63;
}
if (Operation.equals("20061")) {
result = NumberCapacity * 0.60;
}
if (Operation.equals("20071")) {
result = NumberCapacity * 0.56;
}
if (Operation.equals("20091")) {
result = NumberCapacity * 0.55;
}
if (Operation.equals("20111")) {
result = NumberCapacity * 0.54;
}
if (Operation.equals("20131")) {
result = NumberCapacity * 0.52;
}
}
if (NumberCapacity >= 1501 && NumberCapacity <= 2500) {
if (Operation.equals("20041")) {
result = NumberCapacity * 1.59;
}
if (Operation.equals("20051")) {
result = NumberCapacity * 1.52;
}
if (Operation.equals("20061")) {
result = NumberCapacity * 1.44;
}
if (Operation.equals("20071")) {
result = NumberCapacity * 1.36;
}
if (Operation.equals("20091")) {
result = NumberCapacity * 1.35;
}
if (Operation.equals("20111")) {
result = NumberCapacity * 1.34;
}
if (Operation.equals("20131")) {
result = NumberCapacity * 1.32;
}
}
if (NumberCapacity >= 2501) {
if (Operation.equals("20041")) {
result = NumberCapacity * 3.57;
}
if (Operation.equals("20051")) {
result = NumberCapacity * 3.56;
}
if (Operation.equals("20061")) {
result = NumberCapacity * 3.55;
}
if (Operation.equals("20071")) {
result = NumberCapacity * 3.54;
}
if (Operation.equals("20091")) {
result = NumberCapacity * 3.53;
}
if (Operation.equals("20111")) {
result = NumberCapacity * 3.52;
}
if (Operation.equals("20131")) {
result = NumberCapacity * 3.50;
}
}
Result.setText(String.valueOf(result));//Set the value after calculate it
//Set the value after calculate it
}
}
);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
try {
int no = Integer.parseInt(s.toString());
if (no < 9) {
Toast.makeText(this, "Autovehicolul nu trebuie sa fie mai vechi de 10 ani", Toast.LENGTH_SHORT).show();
}
}
catch(NumberFormatException e){}
}
}
在我的片段中
Fragment.class
package com.calculatorvamal.app;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HomeFragment extends Fragment {
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
}
我尝试用另一种方式进行意图和开始活动,但是当活动开始时它给我一个延迟(启动时延迟30,40毫秒)。 免费提出任何建议,谢谢。
答案 0 :(得分:1)
我认为你误解了或者没有读过如何在片段和活动之间进行交流。您应该了解如何执行此操作:https://developer.android.com/training/basics/fragments/communicating.html。
此外,您不应混合Fragment逻辑和Activity逻辑。要从您的谨慎片段及其布局中使用方面或视图(例如按钮单击侦听器和列表视图创建等)执行的操作应包含在Fragment类中。如果由于各种原因需要将结果从一个传递到另一个,那么您可以在单独的实体之间进行通信,如下所示:
将信息从活动传递到片段:
要向片段发送信息,您只需在片段中定义一个方法来接收它并从您的活动中调用该方法:
public class MyActivity extends Activity
{
private MyFragment myFragment;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_holder);
myFragment = new MyFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, myFragment).commit();
}
private void testFragmentCommunication(String message)
{
myFragment.recievedCommunication(message);
}
}
public class MyFragment extends Fragment
{
...
public boolean recievedCommunication(String message)
{
Log.d("MyFragment", "Recieved communication from Activity: " + message);
}
...
}
将信息从片段传递到活动:
但是,如果您需要将信息从Fragment传递回Activity,则需要定义一个接口供他们使用:
public class MyFragment extends Fragment
{
MyActivityInterface activityInterface;
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
activityInterface = (MyActivityInterface) activity;
}
catch (ClassCastException e)
{
Log.e(TAG, "Parent Activity deosn't implement 'MyActivityInterface'");
throw new ClassCastException(activity.toString()
+ " must implement MyActivityInterface");
}
}
public Interface MyAcyivityInterface
{
public void onMessageReceived();
}
private void testActivityCommunication()
{
activityInterface.onMessageReceived(String message);
}
}
public class MyActivity extends Activity implements MyFragment.MyActivityInterface
{
private MyFragment myFragment;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_holder);
myFragment = new MyFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
public void onMessageReceived(String message)
{
Log.d("MyActivity", "Recieved communication from Fragment: " + message);
}
}