所有其他事情都很完美。只有按钮不起作用。
我还在声明中检查了id。它与XML
文件中的相同。
我在onClick()
添加Toast进行检查。
但是吐司不会显示在输出中。
所以onClick()
没有工作。
我使用comment(// --------------------------------)
突出显示了一个按钮代码package com.smartrix.professionals;
public class Fragment_Plus extends Fragment implements CompoundButton.OnCheckedChangeListener{
ToggleButton need,awarness,product;
EditText title,disc,moneyRange,username,professions,city;
String Fusername,Ftitle,Fprofessions,Fdiscription,Ftype,Farea,Fcity;
Spinner area;
TextView inputMoneyRange;
SeekBar money;
Button post;
public Fragment_Plus(){}
// Tab view to select Post type
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Ftype = buttonView.getText().toString();
buttonView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
if (need.getId() == buttonView.getId()) {
awarness.setBackgroundColor(getResources().getColor(R.color.plus_tab));
product.setBackgroundColor(getResources().getColor(R.color.plus_tab));
username.setHint("Enter Username Here");
disc.setVisibility(View.VISIBLE);
professions.setVisibility(View.VISIBLE);
title.setVisibility(View.VISIBLE);
money.setVisibility(View.VISIBLE);
moneyRange.setVisibility(View.VISIBLE);
inputMoneyRange.setVisibility(View.VISIBLE);
city.setVisibility(View.GONE);
} else if (awarness.getId() == buttonView.getId()) {
need.setBackgroundColor(getResources().getColor(R.color.plus_tab));
product.setBackgroundColor(getResources().getColor(R.color.plus_tab));
username.setHint("Enter Username Here");
disc.setVisibility(View.VISIBLE);
professions.setVisibility(View.VISIBLE);
title.setVisibility(View.VISIBLE);
money.setVisibility(View.VISIBLE);
moneyRange.setVisibility(View.VISIBLE);
inputMoneyRange.setVisibility(View.VISIBLE);
city.setVisibility(View.GONE);
} else if (product.getId() == buttonView.getId()) {
need.setBackgroundColor(getResources().getColor(R.color.plus_tab));
awarness.setBackgroundColor(getResources().getColor(R.color.plus_tab));
city.setVisibility(View.VISIBLE);
username.setHint("Enter Shop Name");
disc.setVisibility(View.GONE);
professions.setVisibility(View.GONE);
title.setVisibility(View.GONE);
money.setVisibility(View.GONE);
moneyRange.setVisibility(View.GONE);
inputMoneyRange.setVisibility(View.GONE);
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment__plus,container,false);
need = (ToggleButton) view.findViewById(R.id.need_Toggle);
awarness = (ToggleButton) view.findViewById(R.id.awarness_Toggle);
product = (ToggleButton) view.findViewById(R.id.product_Toggle);
username = (EditText) view.findViewById(R.id.createPostUsername);
title = (EditText) view.findViewById(R.id.createPostTitle);
disc = (EditText) view.findViewById(R.id.createPostDisc);
professions = (EditText) view.findViewById(R.id.createPostProfession);
// -------------------------------------------
post = (Button) view.findViewById(R.id.btn_post);
//--------------------------------------------
moneyRange = (EditText) view.findViewById(R.id.createPostMoneyRange);
inputMoneyRange = (TextView) view.findViewById(R.id.createPostMoney);
money = (SeekBar) view.findViewById(R.id.createPostSetMoney);
area = (Spinner) view.findViewById(R.id.createPostArea);
city = (EditText) view.findViewById(R.id.createPostCity);
ArrayAdapter<String> areaAdapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.postArea));
areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
area.setAdapter(areaAdapter);
// ------------------------------------------------------
post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Toast.makeText(getContext(),"Btn Clicked",Toast.LENGTH_LONG);
if(Ftype == "Need"){
Fusername = username.getText().toString();
Ftitle = title.getText().toString();
Fprofessions = professions.getText().toString();
Fdiscription = disc.getText().toString();
Farea = area.getSelectedItem().toString();
backGroundTask bgTask = new backGroundTask(getContext());
bgTask.execute("add_need",Fusername,Ftitle,Fprofessions,Fdiscription,Ftype,Farea);
} else if(Ftype == "Awarness"){
// Awarness Background task Here
} else if(Ftype == "Product"){
Fusername = username.getText().toString();
Fcity = city.getText().toString();
Farea = area.getSelectedItem().toString();
backGroundTask bgTask = new backGroundTask(getContext());
bgTask.execute("add_shop",Fusername,Fcity,Ftype,Farea);
}
}
});
//----------------------------------------
money.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
inputMoneyRange.setText("Range of Money :: " + money.getProgress() + " to "+ money.getMax());
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
try{
money.setMax(Integer.parseInt(moneyRange.getText().toString()));
} catch(NumberFormatException nfe) {
Toast.makeText(getActivity(), "Not Valid Number", Toast.LENGTH_LONG).show();
}
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
need.setOnCheckedChangeListener(this);
awarness.setOnCheckedChangeListener(this);
product.setOnCheckedChangeListener(this);
return view;
}
}
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.smartrix.professionals.Fragment_Plus">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3">
<ToggleButton
android:id="@+id/need_Toggle"
android:layout_weight="1"
android:textOn="Need"
android:background="@color/plus_tab"
android:textSize="17sp"
android:textOff="@string/postNeedToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ToggleButton
android:id="@+id/awarness_Toggle"
android:layout_weight="1"
android:textSize="17sp"
android:textOn="Awarness"
android:textOff="@string/postAwarnessToggleButton"
android:background="@color/plus_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ToggleButton
android:id="@+id/product_Toggle"
android:layout_weight="1"
android:textSize="17sp"
android:textOn="Product"
android:textOff="@string/postProductToggleButton"
android:background="@color/plus_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/createPostArea" />
<EditText
android:layout_marginLeft="10dp"
android:drawableLeft="@mipmap/bottom_profile"
android:layout_marginTop="10dp"
android:drawablePadding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:hint="@string/post_Username"
android:ems="10"
android:id="@+id/createPostUsername" />
<EditText
android:layout_marginLeft="10dp"
android:drawableLeft="@mipmap/title"
android:layout_marginTop="10dp"
android:drawablePadding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/postTitle"
android:hint="@string/title_of_post"
android:ems="10"
android:id="@+id/createPostTitle" />
<EditText
android:layout_marginLeft="10dp"
android:drawableLeft="@mipmap/ic_action_action_language"
android:layout_marginTop="10dp"
android:drawablePadding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:hint="Enter City Name"
android:ems="10"
android:id="@+id/createPostCity" />
<EditText
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@mipmap/discription"
android:drawablePadding="10dp"
android:inputType="textMultiLine"
android:ems="10"
android:text="@string/discriptionEditText"
android:hint="@string/discription_of_post"
android:lines="5"
android:id="@+id/createPostDisc" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@mipmap/bottom_profile"
android:drawablePadding="10dp"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:ems="10"
android:id="@+id/createPostProfession"
android:hint="Profession you need"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@mipmap/money"
android:drawablePadding="10dp"
android:layout_marginLeft="10dp"
android:inputType="number"
android:ems="10"
android:id="@+id/createPostMoneyRange"
android:hint="Set Maximum money"/>
<SeekBar
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:id="@+id/createPostSetMoney"/>
<TextView
android:id="@+id/createPostMoney"
android:layout_marginLeft="10dp"
android:layout_marginBottom="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Range of Money :: "
android:textSize="18dp"
android:layout_marginRight="10dp"
android:textColor="#000000"/>
<Button
android:id="@+id/btn_post"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Post"/>
</LinearLayout>
</ScrollView>
答案 0 :(得分:2)
您没有看到Toast
的原因是因为您从未在其上调用show()
。它必须看起来像这样
Toast.makeText(getContext(),"Btn Clicked",Toast.LENGTH_LONG).show();
除了在Java中,您不会将字符串与==
进行比较,而是使用equals()
方法进行比较。
答案 1 :(得分:1)
您需要在Toast上添加show()
。即。
Toast.makeText(getContext(),"Btn Clicked",Toast.LENGTH_LONG).show();
显示Toast。
答案 2 :(得分:1)
变化:
Toast.makeText(getContext(),"Btn Clicked",Toast.LENGTH_LONG);
要:
Toast.makeText(getContext(),"Btn Clicked",Toast.LENGTH_LONG).show();