문제

I want to create a custom keyboard inside a custom dialog. It has a Textview on top and 12 buttons bellow (it's a numerical keyboard) What I want is: when the buttons are pressed they update the Textview values. I don't know the right place to put the "OnClickListener" and how to update the Textview...

Here is the keyboard layout:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableRow>

    <TextView
        android:id="@+id/valor"
        android:layout_width="300dp"
        android:layout_height="wrap_content"/>

</TableRow>

<TableRow>
    <Button
        android:id="@+id/btn7"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="7"/>
    <Button
        android:id="@+id/btn8"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="8"/>
    <Button
        android:id="@+id/btn9"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="9"/>
</TableRow>

<TableRow>
    <Button
        android:id="@+id/btn4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="4"/>
    <Button
        android:id="@+id/btn5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="5"/>
    <Button
        android:id="@+id/btn6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="6"/>
</TableRow>

<TableRow>
    <Button
        android:id="@+id/btn1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="1"/>
    <Button
        android:id="@+id/btn2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="2"/>
    <Button
        android:id="@+id/btn3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="3"/>
</TableRow>

<TableRow>
    <Button
        android:id="@+id/btn0"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="0"/>
    <Button
        android:id="@+id/btnP"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="."/>
    <Button
        android:id="@+id/btnOk"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Ok"/>
</TableRow>

And here is the MainActivity:

package com.mycompany.myapp;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Button teclado = (Button)findViewById(R.id.teclado);
        teclado.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                showCustomDialog();
            }
        });
    }

    public void showCustomDialog()
    {
        final Dialog dialog = new Dialog(this);
        dialog.getWindow();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.teclado);
        dialog.show();

        final TextView valor = (TextView) dialog.findViewById(R.id.valor);

        Button btn7 = (Button)dialog.findViewById(R.id.teclado);
        btn7.setOnClickListener(new View.OnClickListener(){
        @Override
            public void onClick(View v){
                valor.setText("Test");
            }
        });
    }
}

Thanks in advance!!

도움이 되었습니까?

해결책

Sorry, I found this implementation strange.

I would suggest using DialogFragment, and you can encapsulate everything in there. http://android-developers.blogspot.ca/2012/05/using-dialogfragments.html

You will be able to use the normal setOnClickListener in that fragment.

Button btn7 = (Button)dialog.findViewById(R.id.btn7); **"why R.id.teclado?"**
btn7.setOnClickListener(this)

And your fragment can implement OnClickListener with switch

@Override
public void onClick(View v)
{
    switch (v.getId())
    {
        case R.id.btn7:
            valor.setText("asdf");
            break;
        case R.id.btn8:
            ...
            break;
        ....
    }
    // TODO Auto-generated method stub

}

다른 팁

Ok I want to give some basic idea regarding this. you can modify this according to your requirement.
Give your layout an id. and use layoutinflater to inflate your xml in dialog.
If you want your xml layout in your dialog box you you can do it like this:

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.your_layout_id_here, (ViewGroup) getCurrentFocus());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
builder.show();

Put above code in Button's onclicklistener. And check Button's view through Switch case like case 1: "this button id" then insert this number in Text view.
Hope this help you

R.id.teclado is not in the dialog layout

Change: Button btn7 = (Button)dialog.findViewById(R.id.teclado);

To: Button btn7 = (Button)dialog.findViewById(R.id.btn7);

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top