gpt4 book ai didi

android - 无法在自定义微调器中选择带有按钮的项目

转载 作者:搜寻专家 更新时间:2023-11-01 07:57:40 26 4
gpt4 key购买 nike

我正在创建一个自定义微调器,其中包含微调器项目中的按钮和 TextView 。在这种情况下,只会检测到按钮的点击。并且未检测到完整项目的点击。但是当我从微调器项目中删除按钮时,单击完整项目可以正常工作。是否可以同时处理两个点击事件?如果是,我们如何实现?

代码如下:-

public class MainActivity extends Activity {

String[] Languages = { "Select a Language", "C# Language", "HTML Language",
"XML Language", "PHP Language" };
// Declaring the Integer Array with resourse Id's of Images for the Spinners
Integer[] images = { 0, R.drawable.image_1, R.drawable.image_2,
R.drawable.image_3, R.drawable.image_4 };

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Declaring and typecasting a Spinner
final Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);

// Setting a Custom Adapter to the Spinner
mySpinner.setAdapter(new MyAdapter(MainActivity.this, R.layout.custom,
Languages));

}

// Creating an Adapter Class
public class MyAdapter extends ArrayAdapter<String> {

public MyAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);

}

public View getCustomView(int position, View convertView,
ViewGroup parent) {

// Inflating the layout for the custom Spinner
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom, parent, false);

// Declaring and Typecasting the textview in the inflated layout
TextView tvLanguage = (TextView) layout
.findViewById(R.id.tvLanguage);

// Setting the text using the array
tvLanguage.setText(Languages[position]);

// Setting the color of the text
tvLanguage.setTextColor(Color.rgb(75, 180, 225));

// Declaring and Typecasting the imageView in the inflated layout
Button img = (Button) layout.findViewById(R.id.imgLanguage);

// Setting an image using the id's in the array
img.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();
}
});

int count = 0;
// Setting Special atrributes for 1st element
if (position == 0) {
// Removing the image view
img.setVisibility(View.GONE);
// Setting the size of the text
tvLanguage.setTextSize(20f);
// Setting the text Color
tvLanguage.setTextColor(Color.BLACK);

count++;
if(count ==0)
tvLanguage.setVisibility(View.INVISIBLE);
}

return layout;
}

// It gets a View that displays in the drop down popup the data at the
// specified position
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {

return getCustomView(position, convertView, parent);
}

// It gets a View that displays the data at the specified position
@Override
public View getView(int position, View convertView, ViewGroup parent) {

return getCustomView(position, convertView, parent);
}

}

public class CustomSpinnerSelection extends Spinner {

private boolean mToggleFlag = true;

public CustomSpinnerSelection(Context context, AttributeSet attrs,
int defStyle, int mode) {
super(context, attrs);
}

public CustomSpinnerSelection(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}

public CustomSpinnerSelection(Context context, AttributeSet attrs) {
super(context, attrs);
}

public CustomSpinnerSelection(Context context, int mode) {
super(context);
}

public CustomSpinnerSelection(Context context) {
super(context);
}

@Override
public int getSelectedItemPosition() {
// this toggle is required because this method will get called in other
// places too, the most important being called for the
// OnItemSelectedListener
if (!mToggleFlag) {
return 0; // get us to the first element
}
return super.getSelectedItemPosition();
}

@Override
public boolean performClick() {
// this method shows the list of elements from which to select one.
// we have to make the getSelectedItemPosition to return 0 so you can
// fool the Spinner and let it think that the selected item is the first
// element
mToggleFlag = false;
boolean result = super.performClick();
mToggleFlag = true;
return result;
}

}

最佳答案

试试这个:

android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"

关于android - 无法在自定义微调器中选择带有按钮的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25718148/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com