我想连接图像按钮以在textview上创建一个单词。我怎样才能做到这一点? 使用OnTouchListener的想法是我认为它更准确"在我的应用程序中下面是我的应用程序的示例图像。 请帮我解决这个问题。我是android编程的新手。我看到了一些使用Motion Event的代码,并且有ACTION_DOWN和ACTION_UP的情况,但是我无法理解它是如何被使用的。 任何有关代码的帮助将受到高度赞赏。
这是我的代码:
public class MainActivity extends Activity {
//VARIABLES HERE
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//R.ID.XML ..
//randomize image display
// DISPLAY TEXT IN TV from IMGBTN
OnClickListener myCommoClickListner = new OnClickListener(){
@Override
public void onClick(View arg0) {
Log.i(TAG,"arg0.getId() = " + arg0.getId());
if(arg0.getId()==R.drawable.a){
Log.i(TAG,"arg0.getId()="+arg0.getId());
generatedString=generatedString+"a"; //[PLACEE RESPACTIVE CHARACTEER HERE]
text.setText(generatedString);
((ImageButton) arg0).setImageResource(R.drawable.changea);
if (!timeHasStarted) {
countDownTimer.start();
timeHasStarted = true;
}
}
}
};
//CUSTOMCLICKLISTENER
}
//COUNTDOWNTIMER
// SEARCH WORD, ADD WORD TO LV (if in database), SCORE
}

XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginTop="4dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp" >
<ImageButton
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world" />
<ImageButton
android:id="@+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world" />
<ImageButton
android:id="@+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world" />
<ImageButton
android:id="@+id/Button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginTop="4dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp" >
<ImageButton
android:id="@+id/Button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world" />
<ImageButton
android:id="@+id/Button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world" />
<ImageButton
android:id="@+id/Button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world" />
<ImageButton
android:id="@+id/Button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginTop="4dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp" >
<ImageButton
android:id="@+id/Button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world" />
<ImageButton
android:id="@+id/Button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world" />
<ImageButton
android:id="@+id/Button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world" />
<ImageButton
android:id="@+id/Button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginTop="4dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp" >
<ImageButton
android:id="@+id/Button13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world" />
<ImageButton
android:id="@+id/Button14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world" />
<ImageButton
android:id="@+id/Button15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world" />
<ImageButton
android:id="@+id/Button16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
&#13;
答案 0 :(得分:1)
我会给你一个想法,因为你明确表示你不知道,所以编写代码是没有意义的,这就是为什么你自己无法提供代码,所以先生,Imagebutton
来自什么我看到这个图像是在一个视图组中,现在去这个文档来了解viewgroups然后得到你的触摸监听器或事件监听器,并将它集中在你的视图组,并获得特定的孩子 - ({{1} })使用forcus,在您的视图组中使用Imagebutton
,然后继续前进..立即获取想法?试试吧..
EDIT1 : - =首先,很棒,我能得到你......但是为什么你不使用gridview并使它们像按钮一样,你可以拥有或能够将它们联系起来通过捕捉被点击的项目并做你所做的事情.. 所以基本上你将字母添加到适配器中的项目(按钮),然后在你的girdview中获取点击的项目这里只是你的适配器中的一些介绍 {{1}然后在你的onclick中通过
获取它Viewgroup.findFocus()
这更容易看到你想要的东西......再加上你可以为getview设计风格..
真实交易(我尝试使用您的代码)
现在来到你的方式..在xml中声明每个图像按钮,将是非常繁琐的.. 这样做,在你的java
中inflate or create a button and add the letter to it by View.setTag((String)"K-your letter")
//然后将您的viewgroup作为对象的setOnclick侦听器..
GridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, final View arg1, final int arg2,long arg3) {
//gettting your letters
String letter = (String) arg1.getTag();
//add it to your word formation using any custom method.
}
});
注意我的结束标签可能是错误的形式检查并尝试它,我从原始手写 我的代码格式也可能是错的,所以检查愚蠢的拼写.. 在创建视图组时,也可以设置focusable,clickable为true .. 让我知道它是否有帮助
答案 1 :(得分:1)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//I am using gridview
GridView gv = (GridView) findViewById(R.id.gridView1);
//this is the adapter for your 16 buttons..
Adapter adapt; // we have not instatiate it yet..
//this is your items to fill yyour adapter ,that is, your random letters
ArrayList<String> letters = new ArrayList<String>(); //add your letters to it..
//using the logic on one of my answers on your questions add 16 different letter string to this letters array.
//i dont think i am pose to cover that right?
//instatiate adapt here below
adapt = new Adapter(context, R.layout.layout_containing_your_imagebutton, letters);
gv.setAdapter(adapt); //setting the adapter
gv.setOnTouchListener(new OnTouchListener() {// this in the ontouch
@Override
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
// this is when the user's hand is on the gridview
//docode can comes here
return true;
}
if(event.getAction() == MotionEvent.ACTION_MOVE){
// this is when the user's hand is moving on the gridview
//docode can comes here
return true;
}
if(event.getAction() == MotionEvent.ACTION_UP){
// this is when the user's hand is raised up;;
return true;
}
return false;
}
});
}
void dococde(ViewGroup v){ // the v here is the arg0 in the touch listener- dont forget to cast it when you reference it
//put docode in one of the touch event that pleases you..
String clicked_or_touch_letter_for_formation_by_user = (String) v.getFocusedChild().getTag();
// add clicked_or_touch_letter_for_formation_by_user to your word adapter.
}
static class Adapter extends ArrayAdapter<String> {
ArrayList<String> list;
Context context;
int resource;
public Adapter(Context context, int resource, ArrayList<String> objects) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
this.list = objects;
this.context = context;
this.resource = resource;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View child = convertView;
RecordHolder holder;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
if (child == null) {
child = inflater.inflate(resource, parent, false);//resource should be one layout with one imagebutton..
// that's the imagebutton you want to inflate 16 times.. or have in your gridview..
holder = new RecordHolder();
holder.letter = (ImageButton) child.findViewById(R.id.yourimagebuttonid); //id to your imagebutton
child.setTag(holder);
}else{
holder = (RecordHolder) child.getTag();
}
final String iLetter = list.get(position);
holder.letter.setTag(iLetter);
return child;
}
}
static class RecordHolder {
ImageButton letter;
}
我有一个忙碌...但只是我正在谈论的想法或逻辑,尝试它,请注意我的结束标签和其他格式可能是坏的或形成错误所以纠正他们与编辑器并添加你各自的ID&# 39; s和布局和尝试,将工作..并且不要忘记upvote或通知它是否帮助你