Android 使用 TextView 的 onClick 跳转到下一页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12096064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:43:27  来源:igfitidea点击:

Using TextView's onClick to go to the next page

androidandroid-layoutandroid-intentandroid-emulator

提问by Shreya

For a better UI, I thought of using TextViews in place of Buttons. When a TextView is clicked, the next page of the application should appear. I have defined onClick functions in the XML file, but it's not working.

为了获得更好的 UI,我想到了使用 TextViews 代替 Buttons。单击 TextView 时,应显示应用程序的下一页。我在 XML 文件中定义了 onClick 函数,但它不起作用。

Here is the XML file.

这是 XML 文件。

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:onClick="serve"
    android:text="@string/Service"
    android:textSize="20dp" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="87dp"
    android:onClick="complain"
    android:text="@string/Comp"
    android:textSize="20dp" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="121dp"
    android:onClick="feed"
    android:text="@string/Feedback"
    android:textSize="20dp" />

Java file:

Java文件:

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void serve(View v)
    {
        Intent i=new Intent();
        i.setClass(this,Second.class);
        startActivity(i);
    }

    public void complain(View v)
    {
        Intent in=new Intent();
        in.setClass(this,Third.class);
        startActivity(in);
    }

    public void feed(View v)
    {
        Intent inn=new Intent();
        inn.setClass(this,Fourth.class);
        startActivity(inn);
    }
}

回答by Sam

A TextView is not clickable by default so the onClick action can never be called... Simply add: android:clickable="true"to each of your TextViews:

默认情况下,TextView 不可点击,因此永远无法调用 onClick 操作...只需将:添加android:clickable="true"到您的每个 TextView:

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:clickable="true"
    android:onClick="serve"
    android:text="@string/Service"
    android:textSize="20dp" />

Buttons and many other Views expect user interaction so they are already clickable, but not a TextView.

按钮和许多其他视图需要用户交互,因此它们已经是可点击的,但不是 TextView。



Also since your onClick() methods perform the same basic action, consider a generic method instead:

此外,由于您的 onClick() 方法执行相同的基本操作,请考虑使用通用方法:

public void click(View v) {
    Intent intent;
    switch(v.getId()) {
    case R.id.serve: // R.id.textView1
        intent = new Intent(this, Second.class);
        break;
    case R.id.complain: // R.id.textView2
        intent = new Intent(this, Third.class);
        break;
    case R.id.feed: // R.id.textView3
        intent = new Intent();
    }
    startActivity(intent);
}

You need to modify your idand onClickattributes in each TextView, like so:

您需要修改每个 TextView 中的idonClick属性,如下所示:

<TextView
    android:id="@+id/serve"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:clickable="true"
    android:onClick="click"
    android:text="@string/Service"
    android:textSize="20dp" />

回答by wsanville

Make sure you have the following methods (with matching signatures) in your Activity. They should each take a parameter that is a View.

确保您的 Activity 中有以下方法(带有匹配的签名)。他们每个人都应该接受一个参数,即View.

public void serve(View v) { }
public void complain(View v) { }
public void feed(View v) { }

回答by bytebender

Your activity (based on your code doesn't have any onClickListeners()You need to obtain a reference to your textViews and then add an event handler and then inside your event handler is where you would call serve(), complain(), feed()See this SO question. Here is an example of changes in your activity class.

您的活动(基于您的代码没有任何onClickListeners()您需要获取对您的 textViews 的引用,然后添加一个事件处理程序,然后在您的事件处理程序中是您将调用的地方serve(), complain(), feed()See this SO question。这是更改的示例你的活动课。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    TextView textView1 = (TextView) findViewById(R.id.TextView1);
    textView1.setOnClickListener(new View.OnClickListener() {

        @Override
        protected void onClick(View view) {

            serve(view);
        }

    });

    //Repeat for each text view...
}

Each TextView will have to have the following to attributes set in xml:

每个 TextView 都必须在 xml 中设置以下属性:

android:onClick="onClick"
android:clickable="true"

Edit:

编辑:

I still don't see where you Activity implements OnclickListenerif you are going to do it that way...

如果您打算这样做,我仍然看不到您 Activity 在哪里实现 OnclickListener......

public class MainActivity extends Activity implements OnclickListener {


    protected void onClick(View view) {
        // borrowed from @Sam
        Intent intent;

        switch(v.getId()) {
            case R.id.serve: // R.id.textView1
                intent = new Intent(this, Second.class);
                break;
            case R.id.complain: // R.id.textView2
                intent = new Intent(this, Third.class);
                break;
            case R.id.feed: // R.id.textView3
                intent = new Intent();
            }

            startActivity(intent);
    }

}