2016-09-30 23 views
1

我的Android應用程序需要一個功能,有一個按鈕需要顯示和隱藏基於一個TextView。這是我的代碼,並進一步解釋。Android studio從條件隱藏和顯示按鈕

public class currentCar extends Fragment implements AsyncResponse, AdapterView.OnItemClickListener { 
final String TAG = this.getClass().getName(); 
private ArrayList<AcceptCars> carList; 
private ListView lvCurrent; 
private FunDapter<AcceptCars> adapter; 
SharedPreferences pref; 

public currentCar() { 
    // Required empty public constructor 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View v = inflater.inflate(R.layout.fragment_current_car, container, false); 

    ImageLoader.getInstance().init(UILConfig.config(getActivity())); 

    lvCurrent = (ListView) v.findViewById(R.id.lvCurrent); 

    pref = getActivity().getSharedPreferences("Login.conf", Context.MODE_PRIVATE); 
    Log.d(TAG, pref.getString("username","")); 

    PostResponseAsyncTask taskRead = new PostResponseAsyncTask(getActivity(), this); 

    taskRead.execute("http://carkila.esy.es/carkila/current.php?acceptCarOwner="+pref.getString("username","")); 

    return v; 
} 

@Override 
public void processFinish(String s) { 
    carList = new JsonConverter<AcceptCars>().toArrayList(s, AcceptCars.class); 

    BindDictionary<AcceptCars> dict = new BindDictionary<AcceptCars>(); 

    dict.addDynamicImageField(R.id.ivImg1, new StringExtractor<AcceptCars>() { 
     @Override 
     public String getStringValue(AcceptCars item, int position) { 
      return item.acceptImage; 
     } 
    }, new DynamicImageLoader() { 
     @Override 
     public void loadImage(String url, ImageView view) { 

      ImageLoader.getInstance().displayImage(url, view); 
     } 
    }); 
    dict.addStringField(R.id.tvCarModel, new StringExtractor<AcceptCars>() { 
     @Override 
     public String getStringValue(AcceptCars item, int position) { 
      return "Car Model: " + item.acceptModel; 
     } 
    }); 
    dict.addStringField(R.id.tvResDate, new StringExtractor<AcceptCars>() { 
     @Override 
     public String getStringValue(AcceptCars item, int position) { 
      return "Date: " + item.acceptDate; 
     } 
    }); 
    dict.addStringField(R.id.tvResTime, new StringExtractor<AcceptCars>() { 
     @Override 
     public String getStringValue(AcceptCars item, int position) { 
      return "Time: " + item.acceptTime; 
     } 
    }); 
    dict.addStringField(R.id.tvResLocation, new StringExtractor<AcceptCars>() { 
     @Override 
     public String getStringValue(AcceptCars item, int position) { 
      return "Location: " + item.acceptResLocation; 
     } 
    }); 
    dict.addStringField(R.id.tvCarType, new StringExtractor<AcceptCars>() { 
     @Override 
     public String getStringValue(AcceptCars item, int position) { 
      return "Car Type: " + item.acceptType; 
     } 
    }); 
    dict.addStringField(R.id.tvCapacity, new StringExtractor<AcceptCars>() { 
     @Override 
     public String getStringValue(AcceptCars item, int position) { 
      return "Capacity: " + item.acceptCapacity; 
     } 
    }); 
    dict.addStringField(R.id.tvFuelType, new StringExtractor<AcceptCars>() { 
     @Override 
     public String getStringValue(AcceptCars item, int position) { 
      return "Fuel Type: " + item.acceptFuelType; 
     } 
    }); 
    dict.addStringField(R.id.tvPlateNumber, new StringExtractor<AcceptCars>() { 
     @Override 
     public String getStringValue(AcceptCars item, int position) { 
      return "Plate Number: " + item.acceptPlateNumber; 
     } 
    }); 
    dict.addStringField(R.id.tvPoster, new StringExtractor<AcceptCars>() { 
     @Override 
     public String getStringValue(AcceptCars item, int position) { 
      return "Posted by : " + item.acceptCarOwner; 
     } 
    }); 
    dict.addStringField(R.id.tvRenter, new StringExtractor<AcceptCars>() { 
     @Override 
     public String getStringValue(AcceptCars item, int position) { 
      return "Rented by: "+ item.renters; 
     } 
    }); 
    dict.addStringField(R.id.tvDestination, new StringExtractor<AcceptCars>() { 
     @Override 
     public String getStringValue(AcceptCars item, int position) { 
      return "Destination: " + item.acceptDestination; 
     } 
    }); 

    dict.addStringField(R.id.tvResPrice, new StringExtractor<AcceptCars>() { 
     @Override 
     public String getStringValue(AcceptCars item, int position) { 
      return "Destination: " + item.acceptPrice; 
     } 
    }); 
    dict.addStringField(R.id.tvresID, new StringExtractor<AcceptCars>() { 
     @Override 
     public String getStringValue(AcceptCars item, int position) { 
      return ""+ item.acceptedID; 
     } 
    }); 
    dict.addStringField(R.id.tvstatus, new StringExtractor<AcceptCars>() { 
     @Override 
     public String getStringValue(AcceptCars item, int position) { 
      return "Status: "+ item.payment; 
     } 
    }); 

    adapter = new FunDapter<>(
      getActivity(), carList, R.layout.layout_rented, dict); 
    lvCurrent.setAdapter(adapter); 
    lvCurrent.setOnItemClickListener(this); 
} 

@Override 
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
    AcceptCars selectedCars = carList.get(i); 
     Intent in = new Intent(getActivity(), ownerStatus.class); 
     in.putExtra("cars", selectedCars); 
     startActivity(in); 
} 
} 

在此listViewitem.payment開始是 「無償」,它會去ownerStatus.class

OwnerStatus.class

public class ownerStatus extends AppCompatActivity { 
TextView tvStatus, tvPaymentStatus, tvresID, tvCarModel, tvCarType, tvCapacity, tvFuelType, tvPlateNumber, tvResDate, tvResTime, tvResLocation, tvPoster, tvRenter, tvDestination, tvPrice; 
ImageView ivImage; 
Button btnPaid; 
Button btnDone; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_owner_status); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    onButtonClick(); 

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    final AcceptCars Cars = (AcceptCars) getIntent().getSerializableExtra("cars"); 
    ImageLoader.getInstance().init(UILConfig.config(ownerStatus.this)); 
    tvCarModel = (TextView) findViewById(R.id.tvCarModel); 
    tvCarType = (TextView) findViewById(R.id.tvCarType); 
    tvCapacity = (TextView) findViewById(R.id.tvCapacity); 
    tvFuelType = (TextView) findViewById(R.id.tvFuelType); 
    tvPoster = (TextView) findViewById(R.id.tvPoster); 
    tvPlateNumber = (TextView) findViewById(R.id.tvPlateNumber); 
    tvResDate = (TextView) findViewById(R.id.tvResDate); 
    tvResTime = (TextView) findViewById(R.id.tvResTime); 
    tvResLocation = (TextView) findViewById(R.id.tvResLocation); 
    tvPrice = (TextView) findViewById(R.id.tvPrice); 
    tvDestination = (TextView) findViewById(R.id.tvDestination); 
    tvRenter = (TextView) findViewById(R.id.tvRenter); 
    tvresID = (TextView) findViewById(R.id.tvresID); 
    tvPaymentStatus = (TextView) findViewById(R.id.tvPaymentStatus); 
    tvStatus = (TextView) findViewById(R.id.tvStatus); 
    ivImage = (ImageView) findViewById(R.id.ivImg1); 

    if (Cars != null) { 
     tvresID.setText("" + Cars.acceptedID); 
     tvCarModel.setText(Cars.acceptModel); 
     tvCarType.setText(Cars.acceptType); 
     tvCapacity.setText(Cars.acceptCapacity); 
     tvFuelType.setText(Cars.acceptFuelType); 
     tvPlateNumber.setText(Cars.acceptPlateNumber); 
     tvResDate.setText(Cars.acceptDate); 
     tvResTime.setText(Cars.acceptTime); 
     tvResLocation.setText(Cars.acceptResLocation); 
     tvPoster.setText(Cars.acceptCarOwner); 
     tvRenter.setText(Cars.renters); 
     tvDestination.setText(Cars.acceptDestination); 
     tvPrice.setText(Cars.acceptPrice); 
     tvPaymentStatus.setText(Cars.payment); 
     tvStatus.setText(Cars.payment); 

     ImageLoader.getInstance().displayImage(Cars.acceptImage, ivImage); 
    } 

} 

public void onButtonClick() { 
    btnPaid = (Button) findViewById(R.id.btnPaid); 
    btnPaid.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      AlertDialog.Builder a_paid = new AlertDialog.Builder(ownerStatus.this); 
      a_paid.setMessage("Does the renter paid the transaction?") 
        .setCancelable(false) 
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialogInterface, int i) { 
          HashMap postData = new HashMap(); 
          postData.put("acceptID", tvresID.getText().toString()); 
          postData.put("payment", "paid"); 
          PostResponseAsyncTask taskPaid = new PostResponseAsyncTask(ownerStatus.this, postData, false, new AsyncResponse() { 
           @Override 
           public void processFinish(String s) { 
            if (s.contains("success")) { 
             Toast.makeText(ownerStatus.this, "renter is paid", Toast.LENGTH_SHORT).show(); 
             Intent in = new Intent(ownerStatus.this, OwnerTabs.class); 
             startActivity(in); 
             finish(); 
            } else { 
             Toast.makeText(getApplicationContext(), "Failed. Zzzz ☻", Toast.LENGTH_LONG).show(); 
            } 
           } 
          }); 
          taskPaid.execute("http://carkila.esy.es/carkila/paid.php"); 
         } 
        }) 
        .setNegativeButton("No", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialogInterface, int i) { 
          dialogInterface.cancel(); 
         } 
        }); 
      AlertDialog alert = a_paid.create(); 
      alert.setTitle("Payment"); 
      alert.show(); 
     } 
    }); 
    //===================================================================================// 
    btnDone = (Button) findViewById(R.id.btnDone); 
    btnDone.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      AlertDialog.Builder a_done = new AlertDialog.Builder(ownerStatus.this); 
      a_done.setMessage("Is the rent is done?") 
        .setCancelable(false) 
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialogInterface, int i) { 
          HashMap postData2 = new HashMap(); 
          postData2.put("acceptID", tvresID.getText().toString()); 
          postData2.put("status", "done"); 
          PostResponseAsyncTask task1 = new PostResponseAsyncTask(ownerStatus.this, postData2, new AsyncResponse() { 
           @Override 
           public void processFinish(String s) { 
            if (s.contains("success")) { 
             Toast.makeText(ownerStatus.this, "renter is paid", Toast.LENGTH_SHORT).show(); 
            } else { 
             Toast.makeText(getApplicationContext(), "Failed. Zzzz ☻", Toast.LENGTH_LONG).show(); 
            } 
           } 
          }); 
          task1.execute("http://carkila.esy.es/carkila/done.php"); 
           } 
          }) 
        .setNegativeButton("No", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialogInterface, int i) { 
          dialogInterface.cancel(); 
         } 
        }); 
      AlertDialog alert = a_done.create(); 
      alert.setTitle("Done"); 
      alert.show(); 
     } 
    }); 
} 
} 

而就在這裏。當Cars.payment未付時,btnPaid將顯示並隱藏btnDone,但當Cars.payment支付時,btnPaid將隱藏,btnDone現在隱藏。 希望你明白我是如何解釋和幫助我解決問題的。謝謝主席先生:)

+0

你可以的onCreate做到這一點很容易()。 –

+0

是你的問題'我如何隱藏一個按鈕'? – brandall

+0

@brandall - 我可以從xml中隱藏一個按鈕,但是我無法做到這一點,我在我的問題上闡述了先生的問題。 –

回答

1

從你的意見,你說你需要檢查:

if(Cars.payment) { 
    btnDone.setVisibility(View.GONE); 
    btnPaid.setVisibility(View.VISIBLE); 
} else { 
    btnPaid.setVisibility(View.GONE); 
    btnDone.setVisibility(View.VISIBLE); 
} 
+0

? –

+0

@BatzPogi無論你在哪裏檢查付費狀態,並最初顯示的意見 – brandall

+0

這是伎倆先生!謝謝youuuuuuu! :)哈哈哈! –

2
btnDone.setVisibility(someCondition? View.GONE : View.VISIBLE); 
btnPaid.setVisibility(!someCondition? View.GONE : View.VISIBLE); 
0

您通過選擇車的意圖,並在OwnerStatus.class得到。因此,在設置選定車輛的值的位置,您只需檢查car.payment並設置按鈕代表狀態的可見性。

例如: -

有一個像下面的代碼之前聲明按鈕: -

btnPaid = (Button) findViewById(R.id.btnPaid); 
btnDone = (Button) findViewById(R.id.btnDone); 


if (Cars != null) { 
     tvresID.setText("" + Cars.acceptedID); 
     tvCarModel.setText(Cars.acceptModel); 
     tvCarType.setText(Cars.acceptType); 
     tvCapacity.setText(Cars.acceptCapacity); 
     tvFuelType.setText(Cars.acceptFuelType); 
     tvPlateNumber.setText(Cars.acceptPlateNumber); 
     tvResDate.setText(Cars.acceptDate); 
     tvResTime.setText(Cars.acceptTime); 
     tvResLocation.setText(Cars.acceptResLocation); 
     tvPoster.setText(Cars.acceptCarOwner); 
     tvRenter.setText(Cars.renters); 
     tvDestination.setText(Cars.acceptDestination); 
     tvPrice.setText(Cars.acceptPrice); 
     tvPaymentStatus.setText(Cars.payment); 
     tvStatus.setText(Cars.payment); 

     ImageLoader.getInstance().displayImage(Cars.acceptImage, ivImage); 

     //here you just check the status of the car payment 

     if(Car.payment.equalsignorecase("unpaid")) 
     { 
     btnDone.setVisibility(View.Gone); 
     btnPaid.setVisibility(View.VISIBLE); 
     } 
     else 
     { 
     // what you want to do here 
     } 


    }